1

Can a string of the form below be evaluated so that it is equivalent to the same "literal" expression?

Example data and code:

df.name = data.frame(col1 = 1:5, col2 = LETTERS[seq(1:5)], col3 = letters[seq(1:5)], stringsAsFactors = FALSE)
col.name = "col2"
row.num = "4"

var1 = str_c("df.name$", col.name,"[",row.num,"]")

> var1
[1] "df.name$col2[4]"

The literal works as expected

> df.name$col2[4]
[1] D

get() is not equivalent:

get(var1)
## Error in get(var1) : object 'df.name$col2[4]' not found

This form of get() "works" but does not solve the problem

get("df.name")$col2[4]
[1] D

Per other posts I've tried eval(parse()) and eval(parse(text())) without success.

I'm trying to create a function that will search (subset) df.name using the col.name passed to the function. I want to avoid writing a separate function for each column name, though that will work since I can code df.name$col2[row.num] as a "literal".

EDIT

The example code should have shown the row.num as type numeric / integer, i.e., row.num = 4

4
  • 2
    Parsing code as strings is never the answer. df.name[[col.name]][as.numeric(row.num)] is a possibility for you. But I think you should ask yourself how you ended up with a character representation of a row number and start there. Commented Sep 20, 2016 at 18:39
  • Near-duplicate: stackoverflow.com/questions/39560780/… Commented Sep 20, 2016 at 18:46
  • if you really need to do this, eval(parse(text=var1)) should work (it works for me). But @RichScriven's solution (but with get(df.name) instead of df.name) should work. Commented Sep 20, 2016 at 18:48
  • @Rich Scriven - the row number comes from a "for loop". The sample code is just to show the construct. The task at hand is not vectorisable since future rows depend on results of earlier rows. Again, I can avoid the paste/str_c via a separate function for each of the columns from which I need to retrieve a value. However, if I can pass the column name as a parameter to the function, well then I need only one function. Commented Sep 20, 2016 at 19:01

2 Answers 2

3

You are almost there:

> eval(parse(text = var1))
[1] "D"

Because parse expecting file by default, you need to specify the text parameter.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm trying to create a function that will search (subset) df.name using the col.name passed to the function.

Set up data:

df.name = data.frame(col1 = 1:5, col2 = LETTERS[1:5], ## seq() is unnecessary
                     col3 = letters[1:5], 
                     stringsAsFactors = FALSE)
col.name = "col2"
row.num = "4"

Solving your ultimate (index the data frame by column name) rather than your proximal (figure out how to use get()/eval() etc.) question: as @RichardScriven points out,

f <- function(col.name,row.num,data=df.name)
   return(data[[col.name]][as.numeric(row.num)])
}

should work. It would probably be more idiomatic if you specified the row number as numeric rather than character, if possible ...

1 Comment

I will edit my question to note that the example row number should, more appropriately, have been numeric().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.