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
Probably the easiest is to use
xtabs:Obviously a flag can be trivially derived from the above: