简体   繁体   English

如何通过插入NA从不同长度的数据帧绑定列?

[英]How to cbind columns from a data frame of differing lengths by inserting NAs?

I have a list L of data frames with two columns each...a key, and a value column. 我有一个数据框架列表L,每个框架有两列...一个键和一个值列。 I would like to create a data frame where the i-th column is the value column for the i-th data frame. 我想创建一个数据框,其中第i列是第i数据框的值列。 The problem is that different data frames in the list L have slightly different keys (the same for the most part but give or take a couple from data.frame to data.frame) so a simple do.call with a cbind won't cut it). 问题在于列表L中的不同数据帧具有略有不同的键(大多数情况下相同,但是从data.frame到data.frame给出或取出一对),因此使用cbind进行的简单do.call不会被剪切它)。 I looped over the list of data frames and took the union of the key columns to get an all encompassing set of keys bigKeySet...what I'd like to do now then is build this data.frame where there is a row for each of the keys in this bigKeySet, and an NA entry in a column if that data.frame did not have an entry for that key. 我遍历了数据帧的列表,并获取了键列的并集,以获得了包含所有键的一组bigKeySet ...我现在想做的就是构建此data.frame,其中每个行都有一行bigKeySet中的键中的一个,如果该data.frame没有该键的条目,则在列中提供NA条目。 I am not sure how to do this, however - any suggestions? 我不确定如何执行此操作-有什么建议吗?

without some sample data I'm not sure but: 没有一些示例数据,我不确定,但是:

merge(df1,df2,all=T,by='keys') 

will combine the two data frames by their keys columns and name the values columns accordingly. 将通过其键列组合两个数据帧,并相应地命名值列。

Assuming you have a function valueForKey() that returns a value for a key (or equivalent statement): 假设您有一个函数valueForKey() ,该函数返回键(或等效语句)的值:

myKeys <- c('foo', 'bar')
bigKeySet <- c('foo', 'baz', 'bar')
result <- as.vector(mapply(function(x) { if (x %in% myKeys) print(valueForKey(x)) else print(NA) }, bigKeySet))

You can bind result to your data frame of interest. 您可以将result绑定到您感兴趣的数据框。 Repeat for all instances of myKeys . myKeys所有实例重复myKeys

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM