0

sorry for the ugly code, but I'm not sure exactly what's going wrong

for (i in 1:1) 
    tab_sector[1:48,i] <- 
        tapply(get(paste("employee",1997-1+i, "[birth<=(1997-1+i)]",sep="")),
               ordered(sic2digit[birth<=(1997-1+i)],levels=tab_sector_list))

# Error in get(paste("employee", 1997 - 1 + i, 
# "[birth<=(1997-1+i))]",  : object 'employee97[birth<=(1997-1+i)]' not found

but the variable is there:

head(employee97[birth<=(1997-1+i)])
# [1] 1 2 2 1 3 4

a simpler version where "employee" is not conditioned by "birth" works

2
  • 2
    Could be nothing, but shouldn't paste("employee",1997-1+i, "[birth<=(1997-1+i)]",sep="") give you employee1997[birth<=(1997-1+i)] rather than employee97[... ? either the code snippet or the error reported by R doesn't match! Commented Nov 22, 2012 at 17:09
  • sorry, my mistake, in an effort to make it simpler I modified the earlier (even uglier) index: "employee",(1997-1901+i) -- apologies for adding a further layer of confusion Commented Nov 23, 2012 at 8:36

2 Answers 2

2

It would help if you told us what you are trying to accomplish.

In your code the get function is looking for a variable whose name is "'employee97[birth<=(1997-1+i)]", the code that works is finding a variable whose name is "employee1997" then subsetting it, those are very different. The get function does not do subsetting.

Part of what you are trying to do is FAQ 7.21, the most important part of which is the end where it suggests storing your data in lists to make accessing easier.

Sign up to request clarification or add additional context in comments.

Comments

2

You can't get an indexed element, e.g. get("x[i]") fails: you need get("x")[i].

Your code is almost too messy too see what's going on, but this is an attempt at a translation:

   for (i in 1:1){
        ind <- 1997-1+i
        v1 <- get(paste0("employee",ind))
        tab_sector[1:48,i] <- tapply(v1[birth<=ind],
             ordered(sic2digit[birth<=ind],levels=tab_sector_list))
    }

1 Comment

get ("x")[i] was indeed the tip I needed -- thanks with apologies for messy code

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.