[R] Count occurances in integers (or strings)
Steven Kennedy
stevenkennedy2263 at gmail.com
Wed Jun 15 23:12:57 CEST 2011
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.
>
More information about the R-help
mailing list