Richard Boonen Richard Boonen - 1 year ago 68
R Question

R variable list for lapply results in error: "argument is not numeric or logical: returning NA"

I'm trying to do something really trivial but I'm running into a wall. I want a vector which stores the means of some variables.

I tried:

vars <- c("eins_d$q05a", "eins_d$q05b", "eins_d$q05c", "eins_d$q05d", "eins_d$q05e", "eins_d$q05f", "eins_d$q05g", "eins_d$q05h", "eins_d$q05i", "eins_d$q05j")
means <- lapply(vars, mean)

I also tried:

means <- numeric()
for (var in c("eins_d$q05a", "eins_d$q05b", "eins_d$q05c", "eins_d$q05d", "eins_d$q05e", "eins_d$q05f", "eins_d$q05g", "eins_d$q05h", "eins_d$q05i", "eins_d$q05j")) {
means <- append(means, mean(var))

In both cases it does not recognise that I want the operation done using the variables but rather it takes the argument as a string and then complains that the argument is not numeric.

However if I do the same but instead of giving the name explicitly I define the list using:

vars <- names(eins_d[5:15])

it works as intended.

What am I doing wrong?

Answer Source

To see clearly what happens, consider that I want the mean of var1 and var2 only from following data frame,

df <- data.frame(var1 = 1:5, var2 = 2:6, var3 = 3:7)

#  var1 var2 var3
#1    1    2    3
#2    2    3    4
#3    3    4    5
#4    4    5    6
#5    5    6    7

#so our vars will be,
vars <- c('var1', 'var2')

lapply(vars, mean) #--> Tries to find the mean of the strings 'var1' and 'var2'
#[1] NA

#[1] NA


lapply(df[vars], mean) #--> Tries to find the mean of columns 'var1' and 'var2' from 'df'
#[1] 3

#[1] 4
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download