Hi, I have a dataframe column from which I want to calculate the number of 1's in each entry. Some column values could, for example, be "0001001000" and "11110000111". To get the number of occurrences from a string I use this: sum(unlist(strsplit(mydata[,"my_column"], "")) == "1") However, as my data is not in string form.. How do I convert it? l tried: lapply(mydata[,"my_column"],toString) but I do not seem to get it right (or at least I do not understand the output format). Also, are there other options? Can I easily calculate the occurrences directly from the integers?
If I understand you correctly, your data column is not a character. class(mydata[,"my_column"]) If it's numeric or a factor, this should work # convert it to character to split apart the individual digits # then convert the digits to numeric, and calculate the sum sapply(strsplit(as.character(mydata[, "my_column"]), ""), function(x) sum(as.numeric(x))) Jean `·.,, ><(((º> `·.,, ><(((º> `·.,, ><(((º> Jean V. Adams Statistician U.S. Geological Survey Great Lakes Science Center 223 East Steinfest Road Antigo, WI 54409 USA http://www.glsc.usgs.gov (GLSC web site) ----- Jay josip.2000 at gmail.com Wed Jun 15 11:09:25 CEST 2011 Hi, I have a dataframe column from which I want to calculate the number of 1's in each entry. Some column values could, for example, be "0001001000" and "11110000111". To get the number of occurrences from a string I use this: sum(unlist(strsplit(mydata[,"my_column"], "")) == "1") However, as my data is not in string form.. How do I convert it? l tried: lapply(mydata[,"my_column"],toString) but I do not seem to get it right (or at least I do not understand the output format). Also, are there other options? Can I easily calculate the occurrences directly from the integers? [[alternative HTML version deleted]]
This should work: mydata$my_column<-as.character(mydata$my_column) sum(unlist(strsplit(mydata[,"my_column"], "")) == "1") On Wed, Jun 15, 2011 at 7:09 PM, Jay <josip.2000 at gmail.com> wrote:> Hi, > > I have a dataframe column from which I want to calculate the number of > 1's in each entry. Some column values could, for example, be > "0001001000" and "11110000111". > > To get the number of occurrences from a string I use this: > sum(unlist(strsplit(mydata[,"my_column"], "")) == "1") > > However, as my data is not in string form.. How do I convert it? l > tried: > lapply(mydata[,"my_column"],toString) > > but I do not seem to get it right (or at least I do not understand the > output format). > > Also, are there other options? Can I easily calculate the occurrences > directly from the integers? > > ______________________________________________ > 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. >