简体   繁体   English

如何从不同长度的表中获取数据

[英]How to rbind data from differing lengths of tables

Ok, I am sure there is a simple solution to this. 好的,我敢肯定有一个简单的解决方案。 Assuming the following data 假设以下数据

A <- 1:10
B <- rep("Part A", 10)
C <- 10:19
df1 <- data.frame(A,B,C)

A <- 1:9
B <- rep("Part B", 9)
D <- 20:28
df2 <- data.frame(A,B,D)

Now I want to create df3 which the user specifies which column names. 现在,我要创建df3 ,用户可以指定该列名称。 So df3 should be a 2*19 data frame of only A and B 因此df3应该是仅AB的2 * 19数据帧

This does not work 这行不通

df3 <- rbind(df1[A,B], df2[A,B])

I dont want to use common_cols or [,x] function as my real dataset has over 1000 variables, that are not always in the same order. 我不想使用common_cols[,x]函数,因为我的实际数据集有1000多个变量,但这些变量并不总是顺序相同的。

Your syntax isn't quite right for subsetting. 您的语法不太适合用于子集。 Try 尝试

cols <- c("A", "B")
rbind(df1[,cols], df2[,cols])

The columns you want to keep should be a vector of names (or indices/logicals) after the , . 你想保留的列后应的名称(或指数/逻辑值)的向量,

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

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