Re: [R] Trouble Combining With Paste

From: Phil Spector <spector_at_stat.berkeley.edu>
Date: Wed, 25 May 2011 09:23:11 -0700 (PDT)

John -

    Try

infert[,toolong] = sapply(infert[,toolong],cut2,g=10,levels.mean=TRUE)

On Wed, 25 May 2011, Sparks, John James wrote:

> Dear R Helpers,
>
> I am having trouble combining some pieces of programming that work fine
> individually, but fall down when I try to get them to work together.
>
> The end goal is to take a data frame, and if any of the variables has more
> than 10 values, then use cut2 to reduce the number of (effective) values
> to 10. I want to do this in automated fashion, which is where the
> combining comes in.
>
> For example all of these pieces work as I would expect:
>
>
> tables<-lapply(infert,table)
> lengths<-lapply(tables,length)
> toolong<-which(lengths>10)
>
> require(Hmisc)
>
> foo<-as.numeric(cut2(infert$age,g=10,levels.mean=TRUE))
> str(foo)
> #num [1:248] 2 10 9 7 7 8 1 6 1 3 ...
>
> bar<-paste("inftert$",attr(toolong[1],"names"),sep="")
> bar
> #[1] "inftert$age"
>
> But the following gives an error:
>
> foobar<-as.numeric(cut2(paste("inftert$",attr(toolong[1],"names"),sep=""),g=10,levels.mean=TRUE))
> Error in min(diff(x.unique))/2 : non-numeric argument to binary operator
> In addition: Warning message:
> In min(diff(x.unique)) : no non-missing arguments, returning NA
>
>
> Your guidance would be much appreciated.
>
> --John J. Sparks, Ph.D.
>
> ______________________________________________
> 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.
>



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. Received on Wed 25 May 2011 - 16:25:32 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 25 May 2011 - 16:40:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive