【问题标题】:R: how to use lapply or for to loop over all values of multiple stringsR:如何使用 lapply 或 for 循环多个字符串的所有值
【发布时间】:2013-11-07 03:20:33
【问题描述】:

我有一个数据集,其中一个变量根据 200 个字母代码 code1<-c("AAA","BBB","DDD","EEE","FFF")、2 个字母代码 code2<-c("Yyy","Zzzzz") 和 41 个数字代码 code3<-seq(1970,2011,1) 分类。

我有一个函数,它根据 code1,code2,code3 的唯一值从数据的每个子集生成一个 8 数向量。所以,我想在这些数据的每个子集上运行几行代码。

完整列表作为数据框data导入,我目前的工作是从数据框中提取数据的每个子集,对其进行分析,然后保存输出

问题是按照这种方案循环遍历code1code2code3的所有值会很麻烦,而且最好还是生成一个输出数据帧, 8 个数字与产生它们的 code1code2code3 的唯一值一起保存。

我确信无需对 code1-code3 和 assign() 的值进行循环就可以做到这一点,但作为一个新手,我恐怕无法将它们完全放在一起。

谢谢--E

补充资料:

这是我正在运行的函数的输出向量的样子,为一个系列手动设置子集:

output1<-fxn(data$input,[which(data$code1=='AAA'&data$code2=='Yyy'&data$code3==1990)])
output2<-fxn2(output1)
str(output2$out[,2]): num [1:8] 0.009 0.648 0.304 0.004 0.445 ... 
output2$out[,2]: [1] 0.009 0.648 0.304 0.004 0.445 36.720 0.000 1.103

补充资料:

为了响应请求,这个伪造的输出数据集近似于我正在寻找的内容——文件的每一行都来自函数 fxn2 的一次完整运行。前 8 列由函数输出;添加最后 3 列以区分 code1code2code3 的唯一值:

> head(data)
   X.x1    x2    x3    x4    x5     x6 x7    x8 code3 code2 code1
1 0.008 0.595 0.185 0.005 0.173 36.744  0 1.102 1970  male  BGR
2 0.004 0.242 0.276 0.005 0.348 46.017  0 1.108 1971  male  BGR
3 0.002 0.553 0.242 0.005 0.247 35.424  0 1.107 1972  male  BGR
4 0.005 0.593 0.270 0.004 0.312 43.701  0 1.105 1973  male  BGR
5 0.009 0.660 0.217 0.005 0.266 37.955  0 1.103 1974  male  BGR
6 0.006 0.347 0.297 0.005 0.411 50.959  0 1.108 1975  male  BGR
> dput(head(data))
structure(list(X.x1 = c(0.008, 0.004, 0.002, 0.005, 0.009, 0.006
), x2 = c(0.595, 0.242, 0.553, 0.593, 0.66, 0.347), x3 = c(0.185, 
0.276, 0.242, 0.27, 0.217, 0.297), x4 = c(0.005, 0.005, 0.005, 
0.004, 0.005, 0.005), x5 = c(0.173, 0.348, 0.247, 0.312, 0.266, 
0.411), x6 = c(36.744, 46.017, 35.424, 43.701, 37.955, 50.959
), x7 = c(0, 0, 0, 0, 0, 0), x8 = c(1.102, 1.108, 1.107, 1.105, 
1.103, 1.108), year = 1970:1975, sex = structure(c(1L, 1L, 1L, 
1L, 1L, 1L), .Label = "male", class = "factor"), iso3 = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = "BGR", class = "factor")), .Names = c("X.x1", 
"x2", "x3", "x4", "x5", "x6", "x7", "x8", "year", "sex", "iso3"
), row.names = c(NA, 6L), class = "data.frame")

【问题讨论】:

  • 您想为 code1、code2 和 code3 值的所有组合运行您的函数吗?
  • 嗨,是的——没错。最后,我确实想为 code1、code2、code3 的每个唯一组合运行一次函数。
  • 你能告诉dput(head(data))head() 的预期输出应该是什么吗?
  • 嗨,休——我已经完成了,输出如上所示。

标签: r for-loop apply


【解决方案1】:

我认为您可以通过这样做来简化您的代码。如果您提供有关所需输出的更多详细信息,我会相应地更新答案。

code1<-c("AAA","BBB","DDD","EEE","FFF")
code2<-c("Yyy","Zzzzz")
code3<-seq(1970,2011,1) 
params <- expand.grid(code1, code2, code3)
names(params) <- c('code1', 'code2', 'code3')

myFunc <- function(code1, code2, code3) {
    ##add your function code here.
  ...
  ...
  return(output2$out[,2])
}

    LL <- mapply(FUN=myFunc, code1 = params$code1, code2 = params$code2, code3 = params$code3)
    result <- split(LL, rep(1:ncol(LL), each = nrow(LL)))
    result <- do.call(rbind, result)
    result <- cbind(result, params)         result <- cbind(result, params) 

【讨论】:

  • 这绝对是一个有用的开始。在 myFunc 内部,如何将其设置为将每次运行的子例程的输出附加到适当命名的输出对象?
  • @iisan7 你能不能也展示一下output2$out[,2] 的样子。答案将取决于此
  • > str(output2$out[,2]): num [1:8] 0.009 0.648 0.304 0.004 0.445 ... >> output2$out[,2]: [1] 0.009 0.648 0.304 0.004 0.445 36.720 0.000 1.103
  • @iisan7 应该可以帮助您入门
  • 优秀。我不得不做一个小的修改: result
猜你喜欢
  • 2016-12-03
  • 2022-01-07
  • 1970-01-01
  • 2019-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
  • 2014-07-16
相关资源
最近更新 更多