这个问题的帖子很少,但没有一个可以帮我解决。 例如 How to split a data frame? How to split a data frame into multiple data frames based on columns
我想要的是根据不同的列号分割数据,例如1,然后我必须拥有与数据帧一样多的列。 2,我必须有一半的数据帧数据(如果它是偶数)
示例数据位于
之下fredTable <- structure(list(Symbol = structure(c(3L, 1L, 4L, 2L, 5L), .Label = c("CASACBM027SBOG",
"FRPACBW027SBOG", "TLAACBM027SBOG", "TOTBKCR", "USNIM"), class = "factor"),
Name = structure(1:5, .Label = c("bankAssets", "bankCash",
"bankCredWk", "bankFFRRPWk", "bankIntMargQtr"), class = "factor"),
Category = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "Banks", class = "factor"),
Country = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "USA", class = "factor"),
Lead = structure(c(1L, 1L, 3L, 3L, 2L), .Label = c("Monthly",
"Quarterly", "Weekly"), class = "factor"), Freq = structure(c(2L,
1L, 3L, 3L, 4L), .Label = c("1947-01-01", "1973-01-01", "1973-01-03",
"1984-01-01"), class = "factor"), Start = structure(c(1L,
1L, 1L, 1L, 1L), .Label = "Current", class = "factor"), End = c(TRUE,
TRUE, TRUE, TRUE, FALSE), SeasAdj = c(FALSE, FALSE, FALSE,
FALSE, TRUE), Percent = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "Fed", class = "factor"),
Source = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "Res", class = "factor"),
Series = structure(c(1L, 1L, 1L, 1L, 2L), .Label = c("Level",
"Ratio"), class = "factor")), .Names = c("Symbol", "Name",
"Category", "Country", "Lead", "Freq", "Start", "End", "SeasAdj",
"Percent", "Source", "Series"), row.names = c("1", "2", "3",
"4", "5"), class = "data.frame")
我想将其拆分为12个数据框,并将它们命名为case 1到12.
所以我的第一个数据框是case1
case_1
TLAACBM027SBOG
CASACBM027SBOG
TOTBKCR
FRPACBW027SBOG
USNIM
第二个数据框是案例2
case_2
bankAssets
bankCash
bankCredWk
bankFFRRPWk
bankIntMargQtr
等
如果我将数字设置为2,我应该有6列(2对),名称为cas_1,case_2直到case_12
我知道分割功能用于分割数据,但我无法弄清楚如何做到这一点
对于我喜欢的第一个案例
split(fredTable, 1:ncol(fredTable))
答案 0 :(得分:1)
我们可以使用
lst <- setNames(lapply(seq_along(fredTable),
function(i) fredTable[i]),
paste('case', seq_along(fredTable), sep="_"))
list2env(lst, envir=.GlobalEnv)