我有这个载体
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来自第二个)最后一行)。知道如何循环这段代码吗?谢谢!
答案 0 :(得分:0)
所以这是未经测试的(因为你没有提供任何示例数据)并且它不是一种好的工作方式,因为我写的函数你将对象分配给全局环境(你通常不应该这样做)。但这是我找到使用所需命名方案的唯一方法。
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
。
编辑:请注意,根据数据的大小,计算可能需要很长时间!
第二次编辑:删除了一些括号。
第3次编辑:这可能是没有global.env-assignment的解决方案。您可以将矢量直接提供给函数,它应该返回一个包含a1
和c1
的列表,这些列表本身就是数据集的列表。
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 <- myfun2(panelcustomers)
。再次,这都是未经测试的。