R 基于父变量名创建数据帧列表
我试图获取一个数据帧并将其转换为包含特定列的数据帧列表R 基于父变量名创建数据帧列表,r,list,dataframe,R,List,Dataframe,我试图获取一个数据帧并将其转换为包含特定列的数据帧列表 dfs <- data.frame(c('apple', 'apple', 'apple', 'apple'), c('pear','pear','pear','pear'),c('5.30','5.50','5.12','5.63'),c('2.12','2.30','2.40','2.13'),c('5.31','5.55','5.20','5.63'),c('2.15','2.35','2.44','2.15')) names(
dfs <- data.frame(c('apple', 'apple', 'apple', 'apple'), c('pear','pear','pear','pear'),c('5.30','5.50','5.12','5.63'),c('2.12','2.30','2.40','2.13'),c('5.31','5.55','5.20','5.63'),c('2.15','2.35','2.44','2.15'))
names(dfs) <- c('apple','pear','price_apple','price_pear','ask_apple','ask_pear')
> dfs
apple pear price_apple price_pear ask_apple ask_pear
1 apple pear 5.30 2.12 5.31 2.15
2 apple pear 5.50 2.30 5.55 2.35
3 apple pear 5.12 2.40 5.20 2.44
4 apple pear 5.63 2.13 5.63 2.15
dfs您可以执行以下操作:
list(
appledf = dfs[, grep("apple", colnames(dfs))],
peardf = dfs[, grep("pear", colnames(dfs))]
)
这就产生了:
[[1]]
apple price_apple ask_apple
1 apple 5.30 5.31
2 apple 5.50 5.55
3 apple 5.12 5.20
4 apple 5.63 5.63
[[2]]
pear price_pear ask_pear
1 pear 2.12 2.15
2 pear 2.30 2.35
3 pear 2.40 2.44
4 pear 2.13 2.15
将
重塑为长文件,然后拆分
:
tmp <- reshape(dfs[-(1:2)], sep="_", direction="long", timevar="fruit", varying=TRUE)
split(tmp, tmp$fruit)
#$apple
# fruit price ask id
#1.apple apple 5.30 5.31 1
#2.apple apple 5.50 5.55 2
#3.apple apple 5.12 5.20 3
#4.apple apple 5.63 5.63 4
#
#$pear
# fruit price ask id
#1.pear pear 2.12 2.15 1
#2.pear pear 2.30 2.35 2
#3.pear pear 2.40 2.44 3
#4.pear pear 2.13 2.15 4
这几乎是完美的,但我有几百种水果。是否要自动创建df?假设基于dfs[,1:200]。谢谢我想最近的邮件比我来得快我不确定我是否理解timevar=“fruit”部分。这个变量来自哪里?它适用于我的示例数据,但不适用于真实数据集。示例如何知道'fruit'来自第1:2列?@Garglesoap-它是用于输出数据集中第一个变量的名称。在这种情况下,“时间”也可能意味着“团体”。原始数据中有变量,如price.[apple/pear]
——本质上是variable.type.[time]
或variable.type.[group]
。例如,你不必设置timevar=
,试试重塑(dfs[-(1:2)],sep=“”,direction=“long”,variable=TRUE)
,看看它是如何被标记为time
的。我在猜测(variable)中得到了“错误”:无法从名称中猜出时变变量。“它给出的向量是1:2列名称吗?”?我的想法是,它应该在'u'@Garglesoap之后匹配-据我所知,数据中的前两列不需要,因为它们只是重复标签。如果您确实需要它们,您可以进行重塑(dfs,sep=“25;”,direction=“long”,timevar=“fruit”,variating=-(1:2))
。variable=
选项只指定具有模式variable\u time
@Garglesoap的所有变量-variable参数就在文档中-?stats::restrape
-“Arguments”部分的第二个条目中。我认为您可能需要使示例数据更能代表实际数据,以便能够正确地调试。在这种情况下,可以迭代实际字符串,从而命名列表。另外,不需要temp
和as.字符<代码>用于(fnamevec中的水果)水果列表[[水果]]=dfs[,grep(水果,名称(dfs))]
非常好!没有想到要在字符串向量上迭代您的错误可能是由于1)具有不在模式“variable\u time”中的变量,因此重塑
无法通过指定的分隔符(sep=“\u”)猜测模式。2) 您的值在一个时间/组中存在,但在另一个时间/组中不存在。也就是说,你有price\u apple/pear
ask\u apple/pear
,然后只有othervar\u apple
而没有pear
组。但是没有一个失败数据的例子,我只是猜测如何解决它。
tmp <- reshape(dfs[-(1:2)], sep="_", direction="long", timevar="fruit", varying=TRUE)
split(tmp, tmp$fruit)
#$apple
# fruit price ask id
#1.apple apple 5.30 5.31 1
#2.apple apple 5.50 5.55 2
#3.apple apple 5.12 5.20 3
#4.apple apple 5.63 5.63 4
#
#$pear
# fruit price ask id
#1.pear pear 2.12 2.15 1
#2.pear pear 2.30 2.35 2
#3.pear pear 2.40 2.44 3
#4.pear pear 2.13 2.15 4
fnamevec <- c('orange', 'pear')
fruitlist <- list()
for(i in 1:2){
temp <- dfs[,grep(as.character(fnamevec[i]), colnames(dfs))]
fruitlist[[i]] <- temp
}