I have a dataframe in a long format with companies and their estimates by regions. I want to build a wide table where I could see how many positive and negative estimates were given to a company by region. When I try pivot_wider I recieve a dataframe with vectors in its cells. That is okay, however I am unable to count the number of positive and negative feedbacks by regions. Also tried using unnest and unnest_longer functions. The latter though seems to resolve my problem, but it takes only one argument for column to unnest.
How can I, may be, modify my pivot_wider to get the desirable result?
My dataframe:
set.seed(1407)
test_df <- data.frame(code = rep(c("positive", "negative"), 9),
company = c("Google", "Amazon", "SpaceX", "BlueOrigin",
"Google", "Western Digital", "Aliexpress",
"Tencent", "Aliexpress"),
n = rbinom(18, size = 9, prob = 0.5),
region = c("Asia", "Europe", "Middle East"))
What I get using a function to wide the table:
test_df %>%
pivot_wider(id_cols = region,
names_from = code,
values_from = n)
# A tibble: 3 x 3
region positive negative
<chr> <list> <list>
1 Asia <int [3]> <int [3]>
2 Europe <int [3]> <int [3]>
3 Middle East <int [3]> <int [3]>
My desirable output:
region positive negative
Asia 4 2
Asia 3 5
Asia 5 2
Europe 3 5
Europe 6 4
Europe 5 1
Middle East 8 5
Middle East 6 5
Middle East 6 2