【问题标题】:For loop creating dataframesFor循环创建数据框
【发布时间】:2017-01-11 11:13:40
【问题描述】:

我有这个向量

panelcustomers <- c(40482, 37244, 17734, 39786, 42613, 45703, 9534, 41772, 42358, 23870, 21815, 29070, 14248, 29484, 12720, 11951, 28290, 9245, 617, 17850, 44827, 29726, 30967, 36267, 37724, 37868, 33041, 37412, 42226, 41701, 26931, 15634, 29180, 35254, 33668, 18337, 31376, 16439, 26222, 7122, 35112, 38161, 23269, 35577, 24493, 1379, 36592, 40487, 8144, 39453, 6361, 34777, 17886, 33273, 11647, 34762, 25881, 5094, 55336, 13427, 28155, 46457, 54933, 42932, 52650, 40607, 15742, 15403, 27240, 28521, 23076, 46817, 39350, 44987, 34671, 53260, 39353, 52295, 56728)

并想使用 for 循环遍历向量,执行以下代码:

pc17734_it <- subset(Paneldataexport,Paneldataexport$V1 == 17734 & Paneldataexport$V2 == "inside_temperature" & Paneldataexport$V3 <= turningpoint['17734',])
pc17734_st <- subset(Paneldataexport,Paneldataexport$V1 == 17734 & Paneldataexport$V2 == "set_point_temperature" & Paneldataexport$V3 <= turningpoint['17734',])
pi17734_it <- subset(Paneldataexport,Paneldataexport$V1 == 17734 & Paneldataexport$V2 == "inside_temperature" & Paneldataexport$V3 > turningpoint['17734',])
pi17734_st <- subset(Paneldataexport,Paneldataexport$V1 == 17734 & Paneldataexport$V2 == "set_point_temperature" & Paneldataexport$V3 > turningpoint['17734',])
interpol_pc17734_it <- approx(pc17734_it$V3, pc17734_it$V4, method = "linear", n=8352, rule = 2)
interpol_pc17734_st <- approx(pc17734_st$V3, pc17734_st$V4, xout =      interpol_pc17734_it$x, method = "constant", rule = 1:2)
interpol_pi17734_it <- approx(pi17734_it$V3, pi17734_it$V4, method = "linear", n=432, rule = 2)
interpol_pi17734_st <- approx(pi17734_st$V3, pi17734_st$V4, xout = interpol_pi17734_it$x, method = "constant", rule = 1:2)
interpol_pc17734_it$st <- interpol_pc17734_st$y
names(interpol_pc17734_it)[names(interpol_pc17734_it) == 'y'] <- 'it'
pc17734 <- interpol_pc17734_it
interpol_pi17734_it$st <- interpol_pi17734_st$y
names(interpol_pi17734_it)[names(interpol_pi17734_it) == 'y'] <- 'it'
pi17734 <- interpol_pi17734_it
remove(pc17734_it, pc17734_st, pi17734_it, pi17734_st, interpol_pc17734_it,     interpol_pc17734_st, interpol_pi17734_it, interpol_pi17734_st)

对于每次迭代,数字(在此示例中为 17734)应替换为向量中的下一个数字 - 以便最终生成的数据帧具有根据数字的名称(在此示例中为第二个 pi17734最后一行)。知道如何遍历这段代码吗?谢谢!

【问题讨论】:

  • 你最终不需要所有的中介,是吗?只有数据集(在本例中为 pi7734)?
  • 是的,只有 pi17734(倒数第二行)和 pc17734(倒数第五行)
  • n-values 是否在整个循环中保持不变?还是可以在子集中访问它们?
  • 对于 pi,n 值始终需要为 432,对于 pc,n 值始终需要为 8352。

标签: r for-loop dataframe


【解决方案1】:

所以这是未经测试的(因为您没有提供任何示例数据)并且它不是一个好的工作方式,因为我编写的函数将对象分配给全局环境(您通常不应该这样做)。但这是我找到的使用您想要的命名方案的唯一方法。

myfun <- function(x) {
    a <- subset(Paneldataexport,Paneldataexport$V1 == x & 
                Paneldataexport$V2 == "inside_temperature" & 
                Paneldataexport$V3 <= turningpoint[paste0(x),])
    b <- subset(Paneldataexport,Paneldataexport$V1 == x &
                Paneldataexport$V2 == "set_point_temperature" &
                Paneldataexport$V3 <= turningpoint[paste0(x),])
    c <- subset(Paneldataexport,Paneldataexport$V1 == x &
                Paneldataexport$V2 == "inside_temperature" &
                Paneldataexport$V3 > turningpoint[paste0(x),])
    d <- subset(Paneldataexport,Paneldataexport$V1 == x &
                Paneldataexport$V2 == "set_point_temperature" &
                Paneldataexport$V3 > turningpoint[paste0(x),])
    a1 <- approx(a$V3, a$V4, method = "linear", n=8352, rule = 2)
    b1 <- approx(b$V3, b$V4, xout = a1$x, method = "constant", rule = 1:2)
    c1 <- approx(c$V3, c$V4, method = "linear", n=432, rule = 2)
    d1 <- approx(d$V3, d$V4, xout = c1$x, method = "constant", rule = 1:2)

    a1$st <- b1$y
    names(a1)[names(a1) == 'y'] <- 'it'
    assign(paste0("pc", x), a1, envir = globalenv())
    c1$st <- d1$y
    names(c1)[names(c1) == 'y'] <- 'it'
    assign(paste0("pi", x), c1, envir = globalenv())
} 

在你的向量上循环这个函数,例如使用sapply(panelcustomers, myfun),最后您应该拥有所有所需的数据框。

更好的方法是将所有数据帧保存在一个列表中,这样您就不需要使用assign

编辑:请注意,根据您的数据大小,这可能需要很长时间来计算!

第二次编辑:删除了一些括号。

第三次编辑:这可能是没有 global.env-assignment 的解决方案。您可以将向量直接输入到函数中,它应该返回一个包含a1c1 的列表,它们本身就是数据集的列表。

myfun2 <- function(x) {
    a <- lapply(x, function(z) subset(Paneldataexport,Paneldataexport$V1 == z & 
                Paneldataexport$V2 == "inside_temperature" & 
                Paneldataexport$V3 <= turningpoint[paste0(z),]))
    b <- lapply(x, function(z) subset(Paneldataexport,Paneldataexport$V1 == z &
                Paneldataexport$V2 == "set_point_temperature" &
                Paneldataexport$V3 <= turningpoint[paste0(z),]))
    c <- lapply(x, function(z) subset(Paneldataexport,Paneldataexport$V1 == z &
                Paneldataexport$V2 == "inside_temperature" &
                Paneldataexport$V3 > turningpoint[paste0(z),]))
    d <- lapply(x, function(x) subset(Paneldataexport,Paneldataexport$V1 == z &
                Paneldataexport$V2 == "set_point_temperature" &
                Paneldataexport$V3 > turningpoint[paste0(z),]))
    a1 <- lapply(a, function(z) approx(z$V3, z$V4, method = "linear", n=8352, rule = 2)
    b1 <- lapply(b, function(z) approx(z$V3, z$V4, xout = a1$x, method = "constant", rule = 1:2)
    c1 <- lapply(c, function(z) approx(z$V3, z$V4, method = "linear", n=432, rule = 2)
    d1 <- lapply(d, function(z) approx(z$V3, z$V4, xout = c1$x, method = "constant", rule = 1:2)

    a1$st <- b1$y
    names(a1)[names(a1) == 'y'] <- 'it'
    c1$st <- d1$y
    names(c1)[names(c1) == 'y'] <- 'it'
    datalist <- list(a1, c1)
    return(datalist)
}

像这样使用它:mydata &lt;- myfun2(panelcustomers)。同样,这一切都未经测试。

【讨论】:

  • 谢谢!需要删除作业 b 和 c 末尾的第二个括号,之后它就可以完美地工作了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-15
  • 2021-09-03
  • 2021-02-12
  • 1970-01-01
  • 1970-01-01
  • 2021-08-01
  • 2019-09-11
相关资源
最近更新 更多