Hi community, I want to count the occurrence of values within a dataframe. data$names is a list of many names. With namelist <- unique(data$names) I get all the existing names. But the result is a factor, not a list of strings. I would then like to go trough all the names in a for-loop and count their occurrence. How do I make this? Best regards, Markus
unique: factor to string
3 messages · Markus Mühlbacher, Dimitris Rizopoulos, David Winsemius
do you mean you want: table(data$names) I hope it helps. Best, Dimitris
Markus M?hlbacher wrote:
Hi community, I want to count the occurrence of values within a dataframe. data$names is a list of many names. With namelist <- unique(data$names) I get all the existing names. But the result is a factor, not a list of strings. I would then like to go trough all the names in a for-loop and count their occurrence. How do I make this? Best regards, Markus
______________________________________________ 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.
Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
On Jan 18, 2010, at 10:05 AM, Markus M?hlbacher wrote:
Hi community, I want to count the occurrence of values within a dataframe. data$names is a list of many names.
It's most likely a vector.
With namelist <- unique(data$names) I get all the existing names. But the result is a factor, not a list of strings.
as.character(data$names) # would be a character vector, i.e., a structure composed of "strings" # You could make it a list if you wanted, but it's not clear that is needed for your purposes.
I would then like to go trough all the names in a for-loop and count their occurrence.
That shows your C programming background. Try instead the loop-less method: table(data$names) BTW using "names" for an object's name is not a good programing practice in R because there is a useful function by the same name. ..... and then there is always the "dog" fortune. -- David.
David Winsemius, MD Heritage Laboratories West Hartford, CT