I have a data frame with variables labeled var1-var38 and I am trying to multiply var1*var20-38 and add these new variables to the existing data frame. My issue is that I can create a string with var20, but it is not referring to var20 in the data frame. My code is as follows:
inter.data <- read.csv("interactions.csv", header = T, sep = ",")
a <- 20
while(a <= 38) {
name <- paste("var", a, sep = "")
inter.data[paste("var1*", name, sep="")] <- NA
inter.data$var1*name <- (inter.data$var1)*(inter.data$name)
a <- a+1
}
I have tried
inter.data <- read.csv("interactions.csv", header = T, sep = ",")
a <- 20
attach(inter.data)
while(a <= 38) {
name <- paste("var", a, sep = "")
inter.data[paste("var1*", name, sep="")] <- NA
inter.data$var1*name <- (var1)*(name)
a <- a+1
}
as well