Hi, I hope this is not too trivial, but I've had this recurring problem and I think there is super easy solution, just not sure what it is. Please see short example below. ?I would like to get the frequency (counts) of all the variables in a single column (that is easy), but I would also like to return the value 0 for the absence of variables defined in another column. For example: animals = matrix(c('cat','tiger','cat','tiger','fish','fish','dog','dog'),ncol=2, byrow=F) animals = as.data.frame(animals) table(animals$V1) # Returns the count for the variables in the first column as cat tiger ? ?2 ? ? 2 # But I would like to have table(animals$V1) return cat tiger ?dog ?fish ? ?2 ? ? 2 ? ?0 ? ? ?0 Any help is much appreciated. Cheers, Dan
This would do it in your example:> levels(animals$V1) <- c("cat","tiger","dog","fish") > table(animals)cat tiger dog fish 2 2 0 0 HTH David cat tiger dog fish 2 2 0 0 On 11 April 2012 14:21, Daniel Gabrieli <daniel.gabrieli@gmail.com> wrote:> animals = as.data.frame(animals) > > table(animals$V1) >[[alternative HTML version deleted]]
On Apr 11, 2012, at 9:21 AM, Daniel Gabrieli wrote:> Hi, > > I hope this is not too trivial, but I've had this recurring problem > and I think there is super easy solution, just not sure what it is. > Please see short example below. I would like to get the frequency > (counts) of all the variables in a single column (that is easy), but I > would also like to return the value 0 for the absence of variables > defined in another column. > > For example: > > animals = > matrix > (c('cat','tiger','cat','tiger','fish','fish','dog','dog'),ncol=2, > byrow=F) > > animals = as.data.frame(animals) > > table(animals$V1) > > # Returns the count for the variables in the first column as > cat tiger > 2 2 > > # But I would like to have table(animals$V1) return > > cat tiger dog fish > 2 2 0 0 >You need to add the missing levels to that factor. > levels(animals$V1) <- unique( c('cat','tiger','cat','tiger','fish','fish','dog','dog')) > table(animals$V1) cat tiger fish dog 2 2 0 0> > Any help is much appreciated. > > Cheers, > > Dan > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT
> I would like to get the frequency > (counts) of all the variables in a single column (that is > easy), but I would also like to return the value 0 for the > absence of variables defined in another column.If you use factor() on your columns and include all the animals in the factor levels, you should get what you want. For example animal.names <- sort(c("fish", "dog", "tiger", "cat")) V1 <- sample(c('cat', 'tiger'), 10, replace=TRUE) V1 <- factor(V1, levels=animal.names) table(V1) For your data frame, you can get animal.names from your existing data set directly rather than specify in advance. If they are all already factors (as they will be if you have used as.data.frame on a character matrix) you can get all the levels using rapply. Re-using factor will again get you what you're after: animals <- matrix(c('cat','tiger','cat','tiger','fish','fish','dog','dog'),ncol=2, byrow=F) animals <- as.data.frame(animals) animal.names <- sort(rapply(animals, levels)) animals2 <- as.data.frame( lapply(animals, factor, levels=animal.names)) table(animals2$V1) For extra safety, you might want to wrap the second factor() round an as.character: animal.names <- sort(rapply(animals, levels)) animals2 <- as.data.frame( lapply(animals, function(x, l) factor(as.character(x), levels=l), l=animal.names)) table(animals2$V1) S Ellison ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}