> df %>%
+ pivot_longer(-ID,names_to = "k",values_to = "v") %>%
+ mutate(new=paste(k,v,sep = "_")) %>%
+ select(ID,new) %>%
+ table() %>%
+ as.data.frame.matrix()
Full tidy:
> df %>%
+ pivot_longer(-ID,names_to = "k",values_to = "v") %>%
+ mutate(new=paste(k,v,sep = "_")) %>%
+ group_by(ID, new) %>%
+ summarise(freq = n()) %>%
+ ungroup() %>%
+ pivot_wider(names_from = new, values_from = freq, values_fill = 0)
with data.table:
df=fread("test.txt", sep = "\t", header = T)
df |>
melt("ID") |>
dcast (ID ~ variable + value, length)
If you want to speed up the computation you can just do
(data > 0) * 1.I think that is not what I wants. It seems they, for every column want to create a new column that says how many 0's, 1's, 2's are in the original column.
yes, that's it!