问题描述
数据集具有大量的 age.year
变量: age.1990,age.1991等.
我有一个字符串值数组> length(age.years)
表示那些变量,以使 age.years [1]
返回"age.1990"
,等等.
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.
我想搜索每个记录的 age.year
变量以找到值 60
.最终,如果 age.1991 [1]
等于60,则新变量 Y.60 [1]
将采用值 Y.1991 [1]
.
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]
.
如何使用数组中的字符串作为变量名称,以避免手工编码每个 var.year
变量? Get()
似乎不起作用.
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??
推荐答案
您可以像使用$一样使用括号中的内容来访问列.
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
)
}
这篇关于使用字符串中的变量名称访问变量值,R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!