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.
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
Hi John, The issue is that: "infert$age" != infert$age One is a text string, the other references the information stored in the age variable of the infert object. If you need to pass the names as a string, use "[" instead: ## for a data frame infert[, "age"] ## for a list infert[["age"]] It looks like from your code maybe: infert[, attr(toolong[1],"names")] HTH, Josh On Wed, May 25, 2011 at 9:02 AM, 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. > > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
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. >