I'm trying to rearrange my data to summarize as a specific format.
Data to summarize and rearrange:
type user
phone A
phone B
email A
email A
phone B
phone A
Format desired:
type A B
phone 2 2
email 2 0
We get the frequency with count
and spread
from 'long' to 'wide'
library(tidyverse)
count(df1, type, user) %>%
spread(user, n, fill = 0)
# A tibble: 2 x 3
# type A B
# <chr> <dbl> <dbl>
#1 email 2 0
#2 phone 2 2
Or using table
from base R
table(df1)
df1 <- structure(list(type = c("phone", "phone", "email", "email", "phone",
"phone"), user = c("A", "B", "A", "A", "B", "A")),
class = "data.frame", row.names = c(NA, -6L))