I want to test the first columns of a data frame for correlation with their last columns. I'm implementing this to a bigger function, so I never know the variable names. Therefore I need an automated solution, which I'm not able to find. I tried the following:
library(lares)
data("iris")
corlist <- list()
for(i in 1:2){
corlist[i] <- corr_var(df = iris,
var = names(iris)[i],
ignore = names(iris)[c(1:2, 5)[-i]],
max_pvalue = 0.05,
top = 50)
}
Which returns:
Error in corr_var(df = iris, var = names(iris)[i], ignore = names(iris)[c(1:2)[-i]], :
Not a valid input: names(iris)[i] was transformed or does not exist.
I tried using get()
, paste()
, or noquote()
, but neither worked. I also tried to insert the column itself like iris[, i]
, but I always receive the same error. The only thing that seems to work is e.g. var = Sepal.Length
and so on, but these can't be looped. \
While we wait for an update on lares::corr_var()
, this should work for you:
corlist <- list()
for(i in 1:2) {
var_name <- names(iris)[i]
var_sym <- rlang::sym(var_name) # Construct symbol from string
corlist[[i]] <- corr_var(
df = iris,
var = !!var_sym, # Unquote and evaluate symbol
ignore = names(iris)[c(1:2, 5)[-i]],
max_pvalue = 0.05,
top = 50
)
}