0

对不起丑陋的代码,但我不确定到底出了什么问题

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

但变量在那里:

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

一个更简单的版本,其中“员工”不受“出生”的影响

4

2 回答 2

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))
    }
于 2012-11-22T17:22:36.897 回答
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.

于 2012-11-22T17:23:50.983 回答