This might be very trivial but I could not find an easy solution anywhere. I am trying to create a script in R to count entries in one column that belong to a one of 3 categories specified another column. I have a list of clinical patients with ID numbers (more than one entry for the same ID) that have been seen by two services (a or b). I need to know how many ID have been seen by service a and by service b and service c, but counting repeated visits by a service only once (so basically the number of patients that have used each service at least once) – hope this makes sense, here is an example to explain.
Example:
ID Category
A001 a
A002 a
A002 a
A002 b
A003 b
A003 b
A005 c
A001 a
A004 b
A004 b
A006 c
A006 aOutput should be something like:
a=3 b=3 c=2This is what I have done, but I am quite stuck, and this might not be good at all!
DataString<- matrix(nrow=dim(refnum)[1], ncol=1) for (i in 1:dim(refnum)[1]){ DataString[i,1]<- paste(refnum[i,], collapse = '') } #generate vector of unique strings uniqueID<- unique(DataString) #create new matrix to store new IDs newID<- matrix(nrow=dim(data)[1], ncol=1) #initiate index n n<-0 #loop through unique strings for (i in 1:dim(refnum)[1]){ #increment n by 1 for each increment through unique strings n<- n+1 #loop through data rows for (j in 1:dim(data)[1]){ #find matches with string i index<- which(DataString == uniqueID[i,1]) #assign new ID to matching rows newID[index,1]<- n } }
One of the many solutions: