我在将一些代码放入函数中/在R中运行循环时遇到了麻烦
我想基于'var99'列中的值替换数据框中的变量(var1,2,3,4)。
我可以通过以下方式执行此操作:
var1 = c(1, 2, 1, 2)
var2 = c(3, 2, 1, 2)
var3 = c(0.4, 2, 1, 2)
var4 = c(1, 2, 1, 2)
n1 = c(10, 14, 12, 10)
n2 = c(5, 3, 12, 10)
var99 = c('se', 'se', 'sd', 'sd')
mydata=data.frame(var1, var2, var3, var4, n1, n2, var99)
mydata<- mutate(mydata, var1 = ifelse(var99=="se",(var1*n1^0.5), var1))
mydata<- mutate(mydata, var2 = ifelse(var99=="se",(var2*n2^0.5), var2))
mydata<- mutate(mydata, var3 = ifelse(var99=="se", (var3*n2^0.5), var3))
mydata<- mutate(mydata, var4 = ifelse(var99=="se", (var4*n2^0.5), var4))
但是使用更多变量将变得笨拙,我更希望使用例如
varnames = c(var1, var2, var3, var4)
然后我将循环浏览。有人能建议我如何构造函数/使用失败吗?
答案 0 :(得分:1)
我希望以下示例可以帮助您为目标选择最佳方法
var1 = c(1, 2, 1, 2)
var2 = c(3, 2, 1, 2)
var3 = c(0.4, 2, 1, 2)
var4 = c(1, 2, 1, 2)
n1 = c(10, 14, 12, 10)
n2 = c(5, 3, 12, 10)
var99 = c('se', 'se', 'sd', 'sd')
mydata=data.frame(var1, var2, var3, var4, n1, n2, var99)
library(dplyr)
# applying one function to those specific 4 columns
mydata %>% mutate_at(vars(var1:var4), funs(ifelse(var99=="se",(.*n2^0.5), .)))
# var1 var2 var3 var4 n1 n2 var99
# 1 2.236068 6.708204 0.8944272 2.236068 10 5 se
# 2 3.464102 3.464102 3.4641016 3.464102 14 3 se
# 3 1.000000 1.000000 1.0000000 1.000000 12 12 sd
# 4 2.000000 2.000000 2.0000000 2.000000 10 10 sd
# applying different functions to different columns
mydata %>%
mutate_at(vars(var1), funs(ifelse(var99=="se",(.*n1^0.5), .))) %>%
mutate_at(vars(var2:var4), funs(ifelse(var99=="se",(.*n2^0.5), .)))
# var1 var2 var3 var4 n1 n2 var99
# 1 3.162278 6.708204 0.8944272 2.236068 10 5 se
# 2 7.483315 3.464102 3.4641016 3.464102 14 3 se
# 3 1.000000 1.000000 1.0000000 1.000000 12 12 sd
# 4 2.000000 2.000000 2.0000000 2.000000 10 10 sd
# applying different functions to those specific 4 columns
mydata %>%
mutate_at(vars(var1:var4), funs(n1 = ifelse(var99=="se",(.*n1^0.5), .),
n2 = ifelse(var99=="se",(.*n2^0.5), .)))
# var1 var2 var3 var4 n1 n2 var99 var1_n1 var2_n1 var3_n1 var4_n1 var1_n2 var2_n2 var3_n2 var4_n2
# 1 1 3 0.4 1 10 5 se 3.162278 9.486833 1.264911 3.162278 2.236068 6.708204 0.8944272 2.236068
# 2 2 2 2.0 2 14 3 se 7.483315 7.483315 7.483315 7.483315 3.464102 3.464102 3.4641016 3.464102
# 3 1 1 1.0 1 12 12 sd 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.0000000 1.000000
# 4 2 2 2.0 2 10 10 sd 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.0000000 2.000000