Intro
A data set has a large number of age.year variables: age.1990, age.1991, etc. I have an array of string values of length(age.years) representing those variables such that age.years[1] returns "age.1990", etc.
Need
I want to search the age.year variables for each record to find the value, 60. Ultimately, if age.1991[1] equals 60, then a new variable Y.60[1] will take the value Y.1991[1].
Question
How do I use strings from arrays as variable names to avoid coding each var.year variable by hand? Get() does not seem to work.
# EXAMPLE CODE
big.data= data.frame(ID= c(1,2), age.1990= c(60, NA),
age.1991= c(61, 60), age.1992= c(62, 61), Y.1990= c(100, 120),
Y.1991= c(NA, 125), Y.1992= c(115, 130), Y.60= c(NA, NA) )
big.data
# ID age.1990 age.1991 age.1992 Y.1990 Y.1991 Y.1992 Y.60
# 1 1 60 61 62 100 NA 115 NA
# 2 2 NA 60 61 120 125 130 NA
age.years = names(big.data)[2:4]
Y.years = names(big.data)[5:7]
age.years[1]= paste0("big.data$", age.years[1])
age.years[1]
# [1] "big.data$age.1990"
summary(age.years[1])
# Length Class Mode
# 1 character character
summary(get(age.years[1]))
# Error in get(age.years[1]) : object 'big.data$age.1990' not found
# Why not found??
big.data[[age.years[1]]]. The double brackets find a single column by name or number.