更新时间:2023-11-15 14:33:22
您可以像使用$一样使用括号中的内容来访问列.
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.
更新:
或者是作者@jtd的替代答案,使用方括号代替粘贴.
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
)
}