a<-data_frame(id=c(rep("A",100),rep("B",78),rep("C",50)),
vall=c(rep("x",100),rep("y",78),rep("x",50)),
val=runif(228))
Now, I´d like to have the following output:
a2<-a %>% group_by(id) %>% count(vall)
table(a2$vall)
but, I´d prefer to have a oneliner and not having to create a2. Is there a smart way of doing this?
This gets the output you want without creating a new table or using the table function. However the output layout is slightly different:
library(tidyverse)
a %>% group_by(id) %>% count(vall) %>% group_by(vall) %>% count()
Does that help at all?
Here is an one-liner with data.table
library(data.table)
setDT(a)[, .N, .(id, vall)][, .N , vall]
Akruns answer is fine, but the question was for dplyr. I just pasted the alternatives from the comments section here for completeness.
a %>% count(vall, id) %>% {table(.$vall)}
count(a, vall, id) %>% count(vall)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.