[R] Trouble Combining With Paste

Sparks, John James jspark4 at uic.edu
Wed May 25 18:02:37 CEST 2011


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.



More information about the R-help mailing list