andrewH
2011-Sep-16 00:49 UTC
[R] Referring to an object by a variable containing its name: 6 failures
Dear Folks-- I'm trying to make a function that takes the columns I select from a data frame and then uses a for loop to print some information about each one, starting with the column name. I succeed in returning the column name, but nothing else I have tried using the variable colName, containing the name of the column, to refer to the column itself has worked. Below I show my non-working function, and a data frame to test it on. I try six distinct ways of trying to turn the variable containing the name back into a name that is recognized by other R functions, mainly functions that display the properties of the object to which the name refers. These are also numbered in the code below in comments: 1. evaluate colName with eval(). 2. convert back into as symbol with as.symbol() 3. treat the data frame as the calling environment using with() 4. use substitute() to plug in any bound information bound in the environment 5. attach the data frame from which the column name is drawn 6. access the column using the $ operator I have actually made this function work using numeric indexing. But I do not understand why none of these ways of accessing the column using its name work. They all give me the properties of the name as a character vector, (except (2), which gives me its properties as a symbol) rather than the properties of the vector to which the name refers. What am I doing wrong? How do I use a variable containing an object's name to refer to the object itself? Although I'm hoping others will find the bald look caused by tearing my hair out to be attractive, I would appreciate any assistance you can offer in understanding this question. Warmly, andrewH testDFcols <- function(data.df, select=c(1:ncol(data.df)), bar=TRUE) { # A useful summary function if(bar) cat("##################################\n"); attach(data.df) for (column in select) { colName <-names(data.df)[column] cat("Column Name(", colName, "): ") # six failures cat(" Class=", class(eval(colName))) #1 cat(" Type=", typeof(as.symbol(colName))) #2 cat(" Length=", length(with(data.df, colName))) #3 cat(" Mode=", mode(substitute(colName)), "\n") #4 cat("Summary:\n") print(summary(colName)) cat("Structure:\n") str(colName) % if (is.factor(data.df$colName)) {cat("Factor Levels: ", levels(data.df$colName),"\n")} else cat("\n") #6 } detach(data.df) invisible(deparse(substitute(data.df))) } A1.df <- { # Makes a test data set A.df with 2-, 3-, & 4-factor sorting variables, making 24 # combinations, & a 4th variable with a unique data value for each combination. # No random component. year.2 <- factor( rep(1:2, each=12) ) cohort.3 <- factor( rep(rep(1:3,each=4),2) ) race.4 <- factor( rep(1:4, 6) ) D1 <- as.numeric(as.character(year.2))*1.1 + as.numeric(as.character(cohort.3))*1.01+ as.numeric(as.character(race.4))*1.001 data.frame(year.2,cohort.3,race.4,D1) } testDFcols(A1.df) -- View this message in context: http://r.789695.n4.nabble.com/Referring-to-an-object-by-a-variable-containing-its-name-6-failures-tp3817129p3817129.html Sent from the R help mailing list archive at Nabble.com.
rmailbox at justemail.net
2011-Sep-16 01:10 UTC
[R] Referring to an object by a variable containing its name: 6 failures
What am I missing about your inquiry: It seems like x[ , colName ] should work: testDFcols <- function(x = data.df, select=c(1:ncol(x)), bar=TRUE) { if(bar) cat("##################################\n"); for (column in select) { colName <-names(x)[column] cat("Column Name(", colName, "): ") cat(" Class=", class(x[,colName])) #1 cat(" Type=", typeof(x[,colName]) ) #2 cat(" Length=", length(x[,colName])) #3 cat(" Mode=", mode(x[,colName]), "\n") #4 cat("Summary:\n") print(summary(x[,colName])) cat("Structure:\n") str(x[,colName]) if (is.factor(x[,colName])) {cat("Factor Levels: ", levels(x[,colName]),"\n")} else cat("\n") #6 } "ugly" } ----- Original message ----- From: "andrewH" <ahoerner at rprogress.org> To: r-help at r-project.org Date: Thu, 15 Sep 2011 17:49:22 -0700 (PDT) Subject: [R] Referring to an object by a variable containing its name: 6 failures Dear Folks-- I'm trying to make a function that takes the columns I select from a data frame and then uses a for loop to print some information about each one, starting with the column name. I succeed in returning the column name, but nothing else I have tried using the variable colName, containing the name of the column, to refer to the column itself has worked. Below I show my non-working function, and a data frame to test it on. I try six distinct ways of trying to turn the variable containing the name back into a name that is recognized by other R functions, mainly functions that display the properties of the object to which the name refers. These are also numbered in the code below in comments: 1. evaluate colName with eval(). 2. convert back into as symbol with as.symbol() 3. treat the data frame as the calling environment using with() 4. use substitute() to plug in any bound information bound in the environment 5. attach the data frame from which the column name is drawn 6. access the column using the $ operator I have actually made this function work using numeric indexing. But I do not understand why none of these ways of accessing the column using its name work. They all give me the properties of the name as a character vector, (except (2), which gives me its properties as a symbol) rather than the properties of the vector to which the name refers. What am I doing wrong? How do I use a variable containing an object's name to refer to the object itself? Although I'm hoping others will find the bald look caused by tearing my hair out to be attractive, I would appreciate any assistance you can offer in understanding this question. Warmly, andrewH testDFcols <- function(data.df, select=c(1:ncol(data.df)), bar=TRUE) { # A useful summary function if(bar) cat("##################################\n"); attach(data.df) for (column in select) { colName <-names(data.df)[column] cat("Column Name(", colName, "): ") # six failures cat(" Class=", class(eval(colName))) #1 cat(" Type=", typeof(as.symbol(colName))) #2 cat(" Length=", length(with(data.df, colName))) #3 cat(" Mode=", mode(substitute(colName)), "\n") #4 cat("Summary:\n") print(summary(colName)) cat("Structure:\n") str(colName) % if (is.factor(data.df$colName)) {cat("Factor Levels: ", levels(data.df$colName),"\n")} else cat("\n") #6 } detach(data.df) invisible(deparse(substitute(data.df))) } A1.df <- { # Makes a test data set A.df with 2-, 3-, & 4-factor sorting variables, making 24 # combinations, & a 4th variable with a unique data value for each combination. # No random component. year.2 <- factor( rep(1:2, each=12) ) cohort.3 <- factor( rep(rep(1:3,each=4),2) ) race.4 <- factor( rep(1:4, 6) ) D1 <- as.numeric(as.character(year.2))*1.1 + as.numeric(as.character(cohort.3))*1.01+ as.numeric(as.character(race.4))*1.001 data.frame(year.2,cohort.3,race.4,D1) } testDFcols(A1.df) -- View this message in context: http://r.789695.n4.nabble.com/Referring-to-an-object-by-a-variable-containing-its-name-6-failures-tp3817129p3817129.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
Timothy Bates
2011-Sep-16 14:09 UTC
[R] Referring to an object by a variable containing its name: 6 failures
On 16 Sep 2011, at 1:49 AM, andrewH wrote:>> I'm trying to make a function that takes column names then >> using the variable colName, containing the name of >> the column, to refer to the column itselfrmailbox at justemail.net said> What am I missing about your inquiry: It seems like x[ , colName ] should work:I think what is being asked is to treat the value of a variable not as a string (which work fine in your example), but as a variable name In php you?d use $$ to dereference by name $whatIwant = 2 $nomdeplume = "whatIwant" $$nomdeplume 2 I?ve not come across this functionality in R: would be great if it exists