Reputation: 107
I’m trying to create 4 new variables from a dataset in R that contains a varying number of observations for each identifier (ID). I want to summarize this information into 2 flags that indicate whether an ID has an observation of type A or B and 2 counter variables containing the total number of A's and B's per ID.
ID<-c(1,1,1,1,1,1,1,2,2,2,2,2,2)
Result<-c('A','A','B','A','A','A','A','B','B','B','B','B','B')
DSN<-data.frame(ID,Result)
The output would be:
ID A_Flag B_Flag A_Count B_Count
1 Y Y 6 1
2 N Y 0 6
Upvotes: 1
Views: 471
Reputation: 13932
Probably the easiest is to use xtabs
:
> xtabs(~ID+Result, DSN)
Result
ID A B
1 6 1
2 0 6
Obviously a flag can be trivially derived from the above:
> xtabs(~ID+Result, DSN) > 0
Result
ID A B
1 TRUE TRUE
2 FALSE TRUE
Upvotes: 1