fcas80
1
Hi, I have a grouped_by and summarized tibble. I can't think of a simple way to convert it to a base R table. The following is a simple example:
factor1 <- c(0,0,0,0,1,1,1,1)
factor2 <- c(0,0,1,1,0,0,1,1)
sales <- c(10,10,15,20,20,30,30,40)
df <- data.frame(factor1, factor2, sales)
suppressMessages(library(dplyr))
df1 <- df %>%
group_by(factor1, factor2) %>%
summarise(total = sum(sales))
df1
factor1 factor2 total
1 0 0 20
2 0 1 35
3 1 0 50
4 1 1 70
What I want is a base R table like this:
0 1
0 20 35
1 50 70
FJCC
2
The numeric column names will be a pain but this is close to what you want.
df1
# A tibble: 4 x 3
# Groups: factor1 [2]
factor1 factor2 total
<dbl> <dbl> <dbl>
1 0 0 20
2 0 1 35
3 1 0 50
4 1 1 70
> library(tidyr)
> pivot_wider(df1,names_from = factor2,values_from = total)
# A tibble: 2 x 3
# Groups: factor1 [2]
factor1 `0` `1`
<dbl> <dbl> <dbl>
1 0 20 35
2 1 50 70
I extend FJCC solution
pivot_wider(df1,
names_from = factor2,
values_from = total) %>%
as.matrix() %>%
as.table()
system
Closed
5
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.