dplyr - Frequency table with double condition in R -


i have data.frame x values count depending of 2 factors: first 1 value of x, second 1 depends on factor. here's sample

set.seed(111) a<-data.frame(x=rnorm(n = 100,mean = 5,sd=1)) a$letter<-sample(letters[1:2],100,replace=t) a$int<-cut(a$x,breaks=c(0,3,6,9)) 

i need frequency value x within interval condition.

count letter = (0,3) x values , letter = b (6,9) values

count both letters (3,6) one

count reverse of first condition

output new data.frame

to make clear

table(a$int,a$letter)            b   (0,3]  2  1   (3,6] 32 49   (6,9]  9  7 

in case, first sum should 2+7, second sum should 1+9 , third sum should 32+49

you can try this:

library(dplyr) as.integer(table(filter(a, letter == 'a')$int) + rev(table(filter(a, letter == 'b')$int))) 

output be:

[1]  9 81 10 

note, don't need dplyr , can filtering differently follows:

as.integer(table(a[a$letter == 'a', ]$int) + rev(table(a[a$letter == 'b', ]$int))) 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -