ManxCat
ManxCat

Reputation: 33

How to run a covariance in for loop in r

I am trying to run a covariance using a for loop but I get an error saying

"is.numeric(x) || is.logical(x) is not TRUE"

I have created two variables to include the data frames.

Var1<-paste("DF",I,"$Column1",sep="")

Var2<-paste("DF",I,"$Column2",sep="")

Cov<-cov(Var1,Var2)

If I hardcode the df and columns the code works correctly, but when trying to make it iterative, it doesn't

The variables are loading correctly but not sure what else I am doing wrong.

Upvotes: 3

Views: 205

Answers (1)

monte
monte

Reputation: 1865

It appears you are trying to use column names as variable in a function. When you are using paste, you are actually not referencing to column name but to a string of column name i.e. "df$column1" is not the same as df$column1.

To use the column name as variable in function you can use other method to reference column name i.e.

f = function(column_name){
  mean(mtcars[[column_name]], na.rm = TRUE)
}

f("mpg")
#> [1] 20.09062

Created on 2021-07-05 by the reprex package (v0.3.0)

If you really wish to use column_name as a variable with $, then following is the way:

f = function(column_name){
  eval(
    substitute(
      mean(mtcars$column_name), 
      list(column_name=as.name(column_name))
    )
  )
}

f("mpg")
#> [1] 20.09062

Created on 2021-07-05 by the reprex package (v0.3.0)

I will try to explain above method briefly, there are three functions to note: eval, substitute and as.name. as.name converts the string to a name (or like an object mtcars), so your column name changes from "mpg" to mpg, but mpg (as an object) does not exist without the dataframe mtcars. So if you were to just refer it like sum(mpg) it will throw an error. substitute' returns an unevaluated expression, which is evaluated using eval. To be honest, this is an advanced stuff, with which I have also struggled to wrap my head around, you can read more about it here: metaprogramming. My advice would be to use [[ to refer column name as variable.

Upvotes: 2

Related Questions