r - Determine the number of datapoints that meet a criteria -


how can find out number of datapoints have in data frame, meet specific criteria?

group<-c("group 1","group 1","group 1","group 2","group 2") factor<-c("factor 1", "factor 1", "factor 2", "factor 1", "factor 2")  data<-data.frame(cbind(group,factor)) data 

for instance, know how many data points in group 1, factor 2. think should able use summary function, can't figure out how specify want combinations of factor levels. instance:

summary(data) #verifies group 1 appears 3 times, , factor 2 appears twice, no information on how many times group 1 , factor 2 appear in same row 

so, how summary table of combinations of different factor levels?

use logical test , sum specific combination:

sum(with(data,group=="group 1" & factor=="factor 2")) [1] 1 

to extend this, use table:

with(data,table(group,factor))           factor group     factor 1 factor 2   group 1        2        1   group 2        1        1 

...which if convert data.frame gives nice little summary dataset:

data.frame(with(data,table(group,factor)))      group   factor freq 1 group 1 factor 1    2 2 group 2 factor 1    1 3 group 1 factor 2    1 4 group 2 factor 2    1 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -