Hello, I am not sure what to write in the subject line, but I would like to take a character string that is a variable in a data frame and apply a function that takes a numeric argument to this character string. Here is a simplified example that would solve my problem. Imagine I have my data stored in a data frame.> x1 <- x2 <- x3 <- x4 <- x5 <- rnorm(20,0,1); > data <- as.data.frame(cbind(x1,x2,x3,x4,x5));I have a vector containing the variables of interest as such.> model.list <- c("x1","x3","x4");> model.list[1][1] "x1" I would like to loop through this vector and apply the floor() function to each variable. In the current form the elements of model.list do not represent the variables in the data frame.> floor(model.list[1])Error in floor(model.list[1]) : Non-numeric argument to mathematical function> floor(eval(model.list[1]))Error in floor(eval(model.list[1])) : Non-numeric argument to mathematical function> s <- expression(paste("floor(",model.list[1],")",sep="")) > sexpression(paste("floor(", model.list[1], ")", sep = ""))> eval(s)[1] "floor(x1)">I have tried the obvious (to me) without success. Perhaps someone could suggest a solution and some tidbits for me to read up on about the how and why. Thanks, Chad R. Bhatti
Try this: # test data set.seed(1) DF <- data.frame(x1 = rnorm(5), x2 = rnorm(5), x3 = rnorm(5)) DF model.list <- c("x2", "x3") # transform for(v in model.list) DF[v] <- floor(DF[v]) On 4/20/06, Chad Reyhan Bhatti <bhatticr at stat.rice.edu> wrote:> Hello, > > I am not sure what to write in the subject line, but I would like to take > a character string that is a variable in a data frame and apply a function > that takes a numeric argument to this character string. > > Here is a simplified example that would solve my problem. > Imagine I have my data stored in a data frame. > > x1 <- x2 <- x3 <- x4 <- x5 <- rnorm(20,0,1); > > data <- as.data.frame(cbind(x1,x2,x3,x4,x5)); > > I have a vector containing the variables of interest as such. > > model.list <- c("x1","x3","x4"); > > > model.list[1] > [1] "x1" > > I would like to loop through this vector and apply the floor() function to > each variable. In the current form the elements of model.list do not > represent the variables in the data frame. > > > floor(model.list[1]) > Error in floor(model.list[1]) : Non-numeric argument to mathematical > function > > > floor(eval(model.list[1])) > Error in floor(eval(model.list[1])) : Non-numeric argument to mathematical > function > > > s <- expression(paste("floor(",model.list[1],")",sep="")) > > s > expression(paste("floor(", model.list[1], ")", sep = "")) > > eval(s) > [1] "floor(x1)" > > > > I have tried the obvious (to me) without success. Perhaps someone could > suggest a > solution and some tidbits for me to read up on about the how and why. > > Thanks, > > Chad R. Bhatti > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
On 4/20/2006 7:02 PM, Chad Reyhan Bhatti wrote:> Hello, > > I am not sure what to write in the subject line, but I would like to take > a character string that is a variable in a data frame and apply a function > that takes a numeric argument to this character string.Remember that dataframes are lists with named members. So to apply something to the x1 member of a dataframe, you can do the usual data$x1 but you can also do data[["x1"]] In addition, dataframes implement the same style of index handling as matrices, so you can refer to the x1 column as data[,"x1"] You could replace "x1" with any expression (e.g. model.list[1]) in either of the latter two examples, e.g. data[[model.list[1]]] or data[,model.list[1]] You say below that you want to loop through the vector; in fact, there's no need to do that. The floor function would work fine on the whole thing at once, e.g. floor(data[,model.list]) Duncan Murdoch> > Here is a simplified example that would solve my problem. > Imagine I have my data stored in a data frame. >> x1 <- x2 <- x3 <- x4 <- x5 <- rnorm(20,0,1); >> data <- as.data.frame(cbind(x1,x2,x3,x4,x5)); > > I have a vector containing the variables of interest as such. >> model.list <- c("x1","x3","x4"); > >> model.list[1] > [1] "x1" > > I would like to loop through this vector and apply the floor() function to > each variable. In the current form the elements of model.list do not > represent the variables in the data frame. > >> floor(model.list[1]) > Error in floor(model.list[1]) : Non-numeric argument to mathematical > function > >> floor(eval(model.list[1])) > Error in floor(eval(model.list[1])) : Non-numeric argument to mathematical > function > >> s <- expression(paste("floor(",model.list[1],")",sep="")) >> s > expression(paste("floor(", model.list[1], ")", sep = "")) >> eval(s) > [1] "floor(x1)" > > I have tried the obvious (to me) without success. Perhaps someone could > suggest a > solution and some tidbits for me to read up on about the how and why. > > Thanks, > > Chad R. Bhatti > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Is this what you are after? floor(data[,model.list]) Or I just didn't understand what you are trying to accomplish? cheers Francisco PS: try to avoid using names that are already reserved to a function like "data" See ?data>From: Chad Reyhan Bhatti <bhatticr at stat.rice.edu> >To: R-help at stat.math.ethz.ch >Subject: [R] the difference between "x1" and x1 >Date: Thu, 20 Apr 2006 18:02:02 -0500 (CDT) > >Hello, > >I am not sure what to write in the subject line, but I would like to take >a character string that is a variable in a data frame and apply a function >that takes a numeric argument to this character string. > >Here is a simplified example that would solve my problem. >Imagine I have my data stored in a data frame. > > x1 <- x2 <- x3 <- x4 <- x5 <- rnorm(20,0,1); > > data <- as.data.frame(cbind(x1,x2,x3,x4,x5)); > >I have a vector containing the variables of interest as such. > > model.list <- c("x1","x3","x4"); > > > model.list[1] >[1] "x1" > >I would like to loop through this vector and apply the floor() function to >each variable. In the current form the elements of model.list do not >represent the variables in the data frame. > > > floor(model.list[1]) >Error in floor(model.list[1]) : Non-numeric argument to mathematical >function > > > floor(eval(model.list[1])) >Error in floor(eval(model.list[1])) : Non-numeric argument to mathematical >function > > > s <- expression(paste("floor(",model.list[1],")",sep="")) > > s >expression(paste("floor(", model.list[1], ")", sep = "")) > > eval(s) >[1] "floor(x1)" > > > >I have tried the obvious (to me) without success. Perhaps someone could >suggest a >solution and some tidbits for me to read up on about the how and why. > >Thanks, > >Chad R. Bhatti > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! >http://www.R-project.org/posting-guide.html