[R] Trouble Combining With Paste
Sarah Goslee
sarah.goslee at gmail.com
Wed May 25 18:11:38 CEST 2011
You need to use get() so that you are acting on the dataframe,
and not the string that names the dataframe.
Sarah
On Wed, May 25, 2011 at 12:02 PM, Sparks, John James <jspark4 at uic.edu> 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.
>
--
Sarah Goslee
http://www.functionaldiversity.org
More information about the R-help
mailing list