I'm currently using case_when
to define a new variable in my data as such:
data[,46] <- NA
data[,46] <- case_when(
data[,35] == 1 ~ data[,36],
data[,35] == 2 ~ data[,37],
data[,35] == 3 ~ data[,38],
data[,35] == 4 ~ data[,39],
data[,35] == 5 ~ data[,40],
data[,35] == 6 ~ data[,41],
data[,35] == 7 ~ data[,42],
data[,35] == 8 ~ data[,43],
data[,35] == 9 ~ data[,44],
data[,35] == 10 ~ data[,45]
)
I'm trying to write a loop to make this function more efficient, but am running into some trouble. Here is what I have attempted:
for (j in 1:10) {
data[,46] <- case_when(
data[,35] == j ~ data[,35+j]
)
}
However, this is returning NAs for all of my values of data[,46]. Any thoughts on what might be going wrong? I would be happy to provide sample data if necessary, but I'm thinking this is more related to me making a simple programming mistake. Thanks in advance!