Let's say I have the data frame below:
x <- data.frame(id= c("a", "b", "c", "d", "e")
, term= c(179, 192, 189, 182, 179)
, f17= c(1, 2, 3, 4, 5)
, s18= c(6, 7, 8, 9, 10)
, f18 = c(11, 12, 13, 14, 15)
, s19 = c(16, 17, 18, 19, 20))
In this data frame, I want to create a variable that records the value for each id from the appropriate column for the given term (f17 corresponds to term 179, s18 corresponds to term 182, f18 corresponds to term 189, f19 corresponds to term 192).
Obviously, this can be done easily as a series of ifelse statements, but every few months I get new terms of data and I don't want to have to manually recode this each time I get more data. Also, I find this kind of coding with a lot of nested ifelse statements to be incredibly difficult to read.
I'm relatively new to R but am a very experienced SAS and SAS macro programmer, so I know that in SAS what I want to do can be done very easily with a couple of arrays and a do loop in a data step, and that's what I'm essentially trying to recreate in R. What I ended up doing was as follows.
NOTE: I realize that what is below isn't the same as a series of nested ifelse statements and is instead a sequence of ifelse statements in order that are over-writing the same variable, but this does get me the solution I need, accounting for all cases in my data.
xTerms <- c(179, 182, 189, 192)
xVars <- c("f17", "s18", "f18", "s19")
x$startVal <- NA
for(i in 1:length(xTerms)){
x$startVal <- ifelse(x$term == xTerms[i], x[[xVars[i]]], x$startVal)
}
I should add, this is the desired result:
> x
id term f17 s18 f18 s19 startVal
1 a 179 1 6 11 16 1
2 b 192 2 7 12 17 17
3 c 189 3 8 13 18 13
4 d 182 4 9 14 19 9
5 e 179 5 10 15 20 5
The idea with the code above being that when I get new data, I simply need to update the definitions of xTerms and xVars. Or I could even have those created dynamically based on a unique list of the values of the term variable in x and the variables in x.
I'm curious to get feedback from more experienced R users if this is the best way to tackle these sorts of iterative problems in R? Are there resources you can share on how to do this sort of a thing that takes better advantage of R?