在 R 中的循环中为变量的特定子集可变地赋值
Variably assign value to particular subset of a variable in a loop in R
再试一次,这是一个更清楚的帐户。
感谢您的回复,但我无法使这些解决方案对我有用;我可能把这件事和我的例子搞混了。我会更具体。
这是我在填充新子集之前需要调整的数据
> clientdata$Burn1
[1] 4 3 3 3 2 2 2 3 2 3 2 4 3 2 2 2 2 3 3 3 4 3 3 2 2 2 3 3 4 2 2 2 1 4 2 4 2 3 3 3 4 2 4 3 3 2 3 2 2 3 4 3 3 2 3 3 2 2 3 2 2 2 2 3 2 3 3
> clientdata$Burn2
[1] 2 3 3 2 2 2 2 3 3 4 2 3 3 2 3 3 3 3 4 3 3 4 3 2 2 2 2 4 3 2 4 2 3 3 3 4 3 3 2 2 4 2 3 2 2 2 3 2 3 3 4 3 3 2 3 2 2 2 2 2 2 2 2 2 2 3 3
> clientdata$Forw2
[1] 3 3 3 3 3 4 3 3 3 4 4 3 4 3 3 3 4 3 3 3 4 2 3 2 2 2 4 4 4 2 4 3 3 4 3 4 3 2 4 3 3 3 3 3 2 3 3 2 2 3 4 3 4 2 3 3 4 2 3 3 3 3 3 3 4 3 3
> clientdata$Harm1
[1] 2 2 2 2 3 2 2 2 2 4 2 3 4 2 3 3 3 3 3 3 2 2 2 2 2 3 4 1 2 2 2 1 1 2 2 4 3 3 2 4 1 3 2 3 2 2 2 2 2 2 4 2 3 2 3 2 2 1 2 2 1 2 2 2 2 1 2
> clientdata$Innov1
[1] 3 3 3 3 4 4 3 2 3 4 4 3 4 3 4 4 4 3 4 3 4 2 3 2 3 3 4 4 4 2 4 3 1 4 4 3 3 3 4 4 4 3 1 4 3 3 4 3 2 3 3 2 4 2 3 1 4 2 3 2 3 1 4 3 3 4 2
这是我的变量名称向量:
> rev.items.new
[1] "clientdata$Harm1_r" "clientdata$Innov1_r" "clientdata$Burn1_r" "clientdata$Forw2_r" "clientdata$Burn2_r"
现在我希望能够做到这一点,但不是针对那些特定子集进行硬编码的方式(它们可能会因下一个客户而改变);基本上用我的列表填充对象名称,现有数据子集的值从 5 中减去。
clientdata$Burn1_r <- 5 - clientdata$Burn1.
clientdata$Burn2_r <- 5 - clientdata$Burn2
clientdata$Forw2_r <- 5 - clientdata$Forw2
clientdata$Harm1_r <- 5 - clientdata$Harm1
clientdata$Innov1_r <- 5 - clientdata$Innov1
输出应该是
> clientdata$Burn1_r
[1] -1 -2 -2 -2 -3 -3 -3 ...
> clientdata$Burn2_r
[1] -3 -2 -2 -3 -3 -3 -3 ...
> clientdata$Forw2_r
[1] -2 -2 -2 -2 -1 -2 -2 ...
> clientdata$Harm1_r
[1] -3 -3 -3 -3 -2 -3 -3 ...
> clientdata$Innov1_r
[1] -2 -2 -2 -2 -1 -1 -2 ...
是不是更清楚了?很抱歉模棱两可,不擅长寻求帮助。
没有 assign
也可以轻松完成,但如果这是为了理解 assign
机制
curr.object <- paste(colnames(data), 'new', sep="_")
for(i in seq_along(curr.object)){
assign('data', `[[<-`(data, curr.object[i], value=5-data[,i]))
}
data
# foo bar doo foo_new bar_new doo_new
#1 9 20 7 -4 -15 -2
#2 14 13 8 -9 -8 -3
#3 3 7 20 2 -2 -15
#4 18 2 12 -13 3 -7
#5 3 6 14 2 -1 -9
更新
以下是更多complex/indirect/unusual等在数据集中创建变量的方法。 IE。首先在全局环境中创建新变量,然后将其分配回数据集。
rev.items.new <- paste(colnames(clientdata), 'r', sep="_")
rev.items.new
#[1] "Burn1_r" "Burn2_r" "Harm1_r" "Innov1_r"
for(i in seq_along(rev.items.new)){
assign(rev.items.new[i], 5-clientdata[,i])
}
head(Burn1_r)
#[1] 1 2 2 2 3 3
clientdata[rev.items.new] <- mget(rev.items.new)
head(clientdata)
# Burn1 Burn2 Harm1 Innov1 Burn1_r Burn2_r Harm1_r Innov1_r
#1 4 2 2 3 1 3 3 2
#2 3 3 2 3 2 2 3 2
#3 3 3 2 3 2 2 3 2
#4 3 2 2 3 2 3 3 2
#5 2 2 3 4 3 3 2 1
#6 2 2 2 4 3 3 3 1
数据
set.seed(25)
data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4, 3, 3, 3, 4, 4, 4, 3, 1,
4, 3, 3, 4, 3, 2, 3, 3, 2, 4, 2, 3, 1, 4, 2, 3, 2, 3, 1, 4, 3,
3, 4, 2)), row.names = c(NA, -67L), .Names = c("Burn1", "Burn2",
"Forw2", "Harm1", "Innov1"), class = "data.frame")
再试一次,这是一个更清楚的帐户。
感谢您的回复,但我无法使这些解决方案对我有用;我可能把这件事和我的例子搞混了。我会更具体。
这是我在填充新子集之前需要调整的数据
> clientdata$Burn1
[1] 4 3 3 3 2 2 2 3 2 3 2 4 3 2 2 2 2 3 3 3 4 3 3 2 2 2 3 3 4 2 2 2 1 4 2 4 2 3 3 3 4 2 4 3 3 2 3 2 2 3 4 3 3 2 3 3 2 2 3 2 2 2 2 3 2 3 3
> clientdata$Burn2
[1] 2 3 3 2 2 2 2 3 3 4 2 3 3 2 3 3 3 3 4 3 3 4 3 2 2 2 2 4 3 2 4 2 3 3 3 4 3 3 2 2 4 2 3 2 2 2 3 2 3 3 4 3 3 2 3 2 2 2 2 2 2 2 2 2 2 3 3
> clientdata$Forw2
[1] 3 3 3 3 3 4 3 3 3 4 4 3 4 3 3 3 4 3 3 3 4 2 3 2 2 2 4 4 4 2 4 3 3 4 3 4 3 2 4 3 3 3 3 3 2 3 3 2 2 3 4 3 4 2 3 3 4 2 3 3 3 3 3 3 4 3 3
> clientdata$Harm1
[1] 2 2 2 2 3 2 2 2 2 4 2 3 4 2 3 3 3 3 3 3 2 2 2 2 2 3 4 1 2 2 2 1 1 2 2 4 3 3 2 4 1 3 2 3 2 2 2 2 2 2 4 2 3 2 3 2 2 1 2 2 1 2 2 2 2 1 2
> clientdata$Innov1
[1] 3 3 3 3 4 4 3 2 3 4 4 3 4 3 4 4 4 3 4 3 4 2 3 2 3 3 4 4 4 2 4 3 1 4 4 3 3 3 4 4 4 3 1 4 3 3 4 3 2 3 3 2 4 2 3 1 4 2 3 2 3 1 4 3 3 4 2
这是我的变量名称向量:
> rev.items.new
[1] "clientdata$Harm1_r" "clientdata$Innov1_r" "clientdata$Burn1_r" "clientdata$Forw2_r" "clientdata$Burn2_r"
现在我希望能够做到这一点,但不是针对那些特定子集进行硬编码的方式(它们可能会因下一个客户而改变);基本上用我的列表填充对象名称,现有数据子集的值从 5 中减去。
clientdata$Burn1_r <- 5 - clientdata$Burn1.
clientdata$Burn2_r <- 5 - clientdata$Burn2
clientdata$Forw2_r <- 5 - clientdata$Forw2
clientdata$Harm1_r <- 5 - clientdata$Harm1
clientdata$Innov1_r <- 5 - clientdata$Innov1
输出应该是
> clientdata$Burn1_r
[1] -1 -2 -2 -2 -3 -3 -3 ...
> clientdata$Burn2_r
[1] -3 -2 -2 -3 -3 -3 -3 ...
> clientdata$Forw2_r
[1] -2 -2 -2 -2 -1 -2 -2 ...
> clientdata$Harm1_r
[1] -3 -3 -3 -3 -2 -3 -3 ...
> clientdata$Innov1_r
[1] -2 -2 -2 -2 -1 -1 -2 ...
是不是更清楚了?很抱歉模棱两可,不擅长寻求帮助。
没有 assign
也可以轻松完成,但如果这是为了理解 assign
机制
curr.object <- paste(colnames(data), 'new', sep="_")
for(i in seq_along(curr.object)){
assign('data', `[[<-`(data, curr.object[i], value=5-data[,i]))
}
data
# foo bar doo foo_new bar_new doo_new
#1 9 20 7 -4 -15 -2
#2 14 13 8 -9 -8 -3
#3 3 7 20 2 -2 -15
#4 18 2 12 -13 3 -7
#5 3 6 14 2 -1 -9
更新
以下是更多complex/indirect/unusual等在数据集中创建变量的方法。 IE。首先在全局环境中创建新变量,然后将其分配回数据集。
rev.items.new <- paste(colnames(clientdata), 'r', sep="_")
rev.items.new
#[1] "Burn1_r" "Burn2_r" "Harm1_r" "Innov1_r"
for(i in seq_along(rev.items.new)){
assign(rev.items.new[i], 5-clientdata[,i])
}
head(Burn1_r)
#[1] 1 2 2 2 3 3
clientdata[rev.items.new] <- mget(rev.items.new)
head(clientdata)
# Burn1 Burn2 Harm1 Innov1 Burn1_r Burn2_r Harm1_r Innov1_r
#1 4 2 2 3 1 3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