Im creating a function to run cor.test for all the columns on a data frame, except the column that is added in the argument. I want the argument (ref) to be the column name, buy when I actually use ref in the cor.test, it says it needs to be a numeric vector. Don't know how to fix it
compute_cor_1 <- function(df, ref){
all_cols <- names(df)
except_col <- ref
include_cols <- all_cols[all_col_ex != except_col]
result <- matrix(NA, nrow = 1, ncol = length(include_cols))
colnames(result) <- include_cols
for(c_name in colnames(result)){
result[,c_name] <- cor.test(df$ref, df[,c_name])$p.value
}
rownames(result) <- names(except_col)
return(result)
}
compute_cor_1(test_data, c1) ## HERE I TRIED IT AND GIVES ERROR