1
使用R base,我想將data.frame轉換爲嵌套列表,同時保持數據名稱不變。以下是我的示例代碼。謝謝。R從Data.Frame製作嵌套列表
#I WOULD LIKE TO SIMPLIFY THIS PROCESS
DF <- expand.grid(NAME = c("FRANK", "TONY", "ED"), DATE = seq(as.Date("2014-01-01"), as.Date("2017-12-31"), by = "day"), NUM = c(1:3))
DF <- list(
lapply(2014:2015, function(t) DF[with(DF, as.Date(paste(t, "01", "01", sep = "-")) <= DATE & DATE <= as.Date(paste(t + 1, "06", "30", sep = "-"))), ]),
lapply(2014:2015, function(t) DF[with(DF, as.Date(paste(t, "01", "01", sep = "-")) <= DATE & DATE <= as.Date(paste(t + 2, "06", "30", sep = "-"))), ]),
lapply(2014:2015, function(t) DF[with(DF, as.Date(paste(t, "01", "01", sep = "-")) <= DATE & DATE <= as.Date(paste(t + 3, "06", "30", sep = "-"))), ])
)
#BUT THIS DOES NOT WORK
DF <- expand.grid(NAME = c("FRANK", "TONY", "ED"), DATE = seq(as.Date("2014-01-01"), as.Date("2017-12-31"), by = "day"), NUM = c(1:3))
DF <- lapply(1:3, function(i) DF[[i]] <- lapply(2014:2015, function(t) DF[with(DF, as.Date(paste(t, "01", "01", sep = "-")) <= DATE & DATE <= as.Date(paste(t + i, "06", "30", sep = "-"))),]))
#YET THIS WORKS WITH A NEW NAME
DF <- expand.grid(NAME = c("FRANK", "TONY", "ED"), DATE = seq(as.Date("2014-01-01"), as.Date("2017-12-31"), by = "day"), NUM = c(1:3))
DF.NEW <- list()
DF.NEW <- lapply(1:3, function(i) DF.NEW[[i]] <- lapply(2014:2015, function(t) DF[with(DF, as.Date(paste(t, "01", "01", sep = "-")) <= DATE & DATE <= as.Date(paste(t + i, "06", "30", sep = "-"))),]))
對我不起作用:'對象「日期」不found' – herbaman
'對象,日期也不found',有一個在你的代碼中的錯誤。你不能提供一個更小的例子嗎?否則,DF [[i]] < - 看起來毫無用處。 –