Pass variable to function R

I can’t understand what is missing. My search for an answer did not lead to what I consider to be a similar problem. Any help is appreciated!

> test
[1] "temp$nitrate"
> mean(temp2$nitrate,na.rm=TRUE)
[1] 1.280833
> mean(test,na.rm=TRUE)
[1] NA
Warning message:
In mean.default(test, na.rm = TRUE) :
  argument is not numeric or logical: returning NA
>

How to make the middle function read a variable?

+4
source share
2 answers

I think you want

mean(eval(parse(text = test)))
+6
source

This also works, and I think it is a lot easier :)

test <- 1:5
get("test")
0
source

Source: https://habr.com/ru/post/1536281/


All Articles