【发布时间】: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。