Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to evaluate a call in a data.frame?

Tags:

dataframe

r

In one package I'm working on, I'm bumping into a problem that looks simple - but I can't figure it out:

A subfunction is given two arguments:

  • obj, a data.frame
  • foo, a call

For example:

> head(obj)
  cadmium copper lead zinc  elev
1    11.7     85  299 1022 7.909
2     8.6     81  277 1141 6.983
3     6.5     68  199  640 7.800
4     2.6     81  116  257 7.655
5     2.8     48  117  269 7.480
6     3.0     61  137  281 7.791
> foo
log(cadmium)
> class(foo)
[1] "call"

In that example, I want to create a vector x <- log(obj$cadmium). How do I do that? I tried using with() but I don't get the expected result:

> with(obj, foo)
log(cadmium)

foo is a call created by the user by specifying a transformation on a column of the data.frame obj:

my_function(obj, foo = log(cadmium)) { ... }

dput() of data snippet:

obj <- structure(list(cadmium = c(11.7, 8.6, 6.5, 2.6, 2.8, 3), copper = c(85L, 
81L, 68L, 81L, 48L, 61L), lead = c(299L, 277L, 199L, 116L, 117L, 
137L), zinc = c(1022L, 1141L, 640L, 257L, 269L, 281L), elev = c(7.909, 
6.983, 7.8, 7.655, 7.48, 7.791)), .Names = c("cadmium", "copper", 
"lead", "zinc", "elev"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6"))
like image 216
Pierre Avatar asked Jun 28 '11 22:06

Pierre


People also ask

How do I get the value of a column in a data frame?

get_value() function is used to quickly retrieve the single value in the data frame at the passed column and index. The input to the function is the row label and the column label.

How do you determine the number of values in a data frame?

We can count by using the value_counts() method. This function is used to count the values present in the entire dataframe and also count values in a particular column.

How do you check data frames?

To check the data type in pandas DataFrame we can use the “dtype” attribute. The attribute returns a series with the data type of each column. And the column names of the DataFrame are represented as the index of the resultant series object and the corresponding data types are returned as values of the series object.

How do you iterate through a column in a data frame?

You can use the for loop to iterate over columns of a DataFrame. You can use multiple methods to iterate over a pandas DataFrame like iteritems() , getitem([]) , transpose(). iterrows() , enumerate() and NumPy. asarray() function.


2 Answers

You need to evaluate the call, for example using eval():

foo <- call("log", quote(cadmium))
with(obj, eval(foo))

which gives:

> with(obj, eval(foo))
[1] 2.4595888 2.1517622 1.8718022 0.9555114 1.0296194 1.0986123

where obj is the snippet of data you showed.

eval() also has an envir argument indicating the environment within which the expression is evaluated. As such you can do what you want without with() using eval() directly:

> eval(foo, envir = obj)
[1] 2.4595888 2.1517622 1.8718022 0.9555114 1.0296194 1.0986123
like image 113
Gavin Simpson Avatar answered Nov 02 '22 13:11

Gavin Simpson


Something like

z <- data.frame(a1=1:5,b1=LETTERS[1:5],c1=letters[1:5])
foo <- quote(log(a1))
eval(foo,envir=z)

but beware: once you start using eval you are descending into the lower depths of R. I still don't fully understand the distinctions among calling frames, enclosing frames, etc. etc. ...

like image 20
Ben Bolker Avatar answered Nov 02 '22 14:11

Ben Bolker