Preface: I am a beginner to R that is eager to learn. Please don't mistake the simplicity of the question (if it is a simple answer) for lack of research or effort!
I have a data.table named data with columns labelled V1 through V20. I want to take the correlation of certain columns (1,6,7,9,10 and 11) relative to V18 such that I have a new table that looks like the following
Variable Correlation_to_V18
V1 cor(V1,V18)
V6 cor(V6,V18)
V7 cor(V7,V18)
V9 cor(V9,V18)
V10 cor(V10,V18)
V11 cor(V11,V18)
I tried using a for loop to no avail
column <- c(1,6,7,9,10,11)
for (i in column) {
correlations<-cor(data[,18], data[,as.numeric(i)])
cor_table<- data.table(variables = colnames(data[,as.numeric(column)]), correlation_val = correlations)
return(cor_table)
}