2

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??
3
  • Try big.data[[age.years[1]]]. The double brackets find a single column by name or number. Commented Feb 18, 2015 at 17:30
  • Just curious, I added your edit with due credit and you removed the answer marker. Do you still need help with the question? Or maybe the edits cancel it out? Can't say I'm not new to SO yet, so I'm being curious. Commented Feb 19, 2015 at 7:30
  • For future reference. If the issue is indeed solved, then the benefit of marking it so is that it doesn't appear in the "unanswered questions" section anymore which in case of R is rather crowded anyway. But do find your own way. Commented Feb 20, 2015 at 11:48

1 Answer 1

1

You can just use paste in brackets to access the column as you would otherwise do with $.

big.data[paste0(age.years[1])]

Additionally, you can use just the numbers to access the columns like this.

years <- c(1990:1992)
big.data[paste0("age.",years[1])]

And the loop will work like this.

for (iy in 1:length(years)){
  big.data$Y.60 <- NA
  big.data$Y.60  <- ifelse(big.data[paste0("age.",years[iy])] == 60,     +
     paste0("Y.",years[iy]),big.data$Y.60 ) 
 }

If I understood your purpose correctly.

Update:

Or an alternative answer from the author @jtd with brackets instead of paste.

for (iy in 1:length(age.years)) {
   big.data$Y = ifelse(big.data[[age.years[iy]]] == 60,
              big.data[[Y.years[iy]]],
              big.data$Y
              )
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.