r - Count the number of instances where a variable or a combination of variables are TRUE -
i'm enthusiastic r newbie needs help! :)
i have data frame looks this:
id<-c(100,200,300,400) a<-c(1,1,0,1) b<-c(1,0,1,0) c<-c(0,0,1,1) y=data.frame(id=id,a=a,b=b,c=c)
where id unique identifier (e.g. person) , a, b , c dummy variables whether person has feature or not (as 1=true).
i want r create matrix or data frame have variables a, b , c both names of columns , of rows. values of matrix r have calculate number of identifiers have feature, or combination of features.
so example, ids 100, 200 , 400 have feature in diagonal of matrix , cross, r input 3. id 100 has both features , b, hence r input 1 , b cross, , forth.
the resulting data frame have this:
l<-c("","a","b","c") m<-c("a",3,1,1) n<-c("b",1,2,1) o<-c("c",1,1,2) result<-matrix(c(l,m,n,o),nrow=4,ncol=4)
as data set has 10 variables , hundreds of observations, have automate whole process.
your appreciated. lot!
with base r:
crossprod(as.matrix(y[,-1])) # b c # 3 1 1 # b 1 2 1 # c 1 1 2
Comments
Post a Comment