[R] Trouble Combining With Paste

Phil Spector spector at stat.berkeley.edu
Wed May 25 18:23:11 CEST 2011


John -
    Try

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


 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu


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.
>



More information about the R-help mailing list