Currently I am trying to merge two tables in R together. Both of them have different contents and no ID, just the columns have numbers given by R.
目前我正在尝试将R中的两个表合并在一起。它们都有不同的内容而没有ID,只有列有R给出的数字。
My problem now is that I cannot add the columns and their values from table 2 to table 1. I also want to mention that both of them have the same amount of rows, which means table 1 has got 1000 rows and and table 2 as well. I also cannot add an ID field otherwise it is not possible to run further steps of my code.
我现在的问题是我无法将表2及其值从表2添加到表1.我还想提到它们都有相同的行数,这意味着表1有1000行,而表2也是。我也无法添加ID字段,否则无法运行我的代码的更多步骤。
Basically my tables look like this:
基本上我的表看起来像这样:
I would really appreciate it, if someone can help me.
如果有人能帮助我,我真的很感激。
2 个解决方案
#1
1
The simplest (and perhaps blind way) is to use cbind to combine the two tables, as long as the number of rows in each table are equal.
最简单(也许是盲目的)是使用cbind来组合两个表,只要每个表中的行数相等即可。
x<-tribble(~Value1, ~Value2, ~Value3,
a,b,c,
aa,bb,cc)
y<-tribble(~Value4, ~Value5, ~Value6,
d,e,f,
dd,ee,ff)
cbind(x,y)
Output becomes
Value 1 Value 2 Value 3 Value 4 Value 5 Value 6
1 a b c d e f
2 aa bb cc dd ee ff
Since the two tables are (I assume) mutually exclusive, there is no way to meaningfully join them if you don't have relations to work with. If you seek to merge them in R, it will merge the two tables and return a dataframe that has all the different unique combinations of merging them. This means that, if you have 1000 rows in each, you may end up with a 1000*1000 dataframe.
由于这两个表(我假设)是互斥的,如果你没有合作关系,就没有办法有意义地加入它们。如果您想要在R中合并它们,它将合并两个表并返回一个具有合并它们的所有不同唯一组合的数据帧。这意味着,如果每个行中有1000行,则最终可能会得到1000 * 1000的数据帧。
#2
1
This will reproduce your example
这将重现您的示例
Value1=c("a","aa")
Value2=c("b","bb")
Value3=c("c","cc")
Value4=c("d","dd")
Value5=c("e","ee")
Value6=c("f","ff")
table1=data.frame(Value1,Value2,Value3)
table2=data.frame(Value4,Value5,Value6)
Result=cbind(table1,table2)
#1
1
The simplest (and perhaps blind way) is to use cbind to combine the two tables, as long as the number of rows in each table are equal.
最简单(也许是盲目的)是使用cbind来组合两个表,只要每个表中的行数相等即可。
x<-tribble(~Value1, ~Value2, ~Value3,
a,b,c,
aa,bb,cc)
y<-tribble(~Value4, ~Value5, ~Value6,
d,e,f,
dd,ee,ff)
cbind(x,y)
Output becomes
Value 1 Value 2 Value 3 Value 4 Value 5 Value 6
1 a b c d e f
2 aa bb cc dd ee ff
Since the two tables are (I assume) mutually exclusive, there is no way to meaningfully join them if you don't have relations to work with. If you seek to merge them in R, it will merge the two tables and return a dataframe that has all the different unique combinations of merging them. This means that, if you have 1000 rows in each, you may end up with a 1000*1000 dataframe.
由于这两个表(我假设)是互斥的,如果你没有合作关系,就没有办法有意义地加入它们。如果您想要在R中合并它们,它将合并两个表并返回一个具有合并它们的所有不同唯一组合的数据帧。这意味着,如果每个行中有1000行,则最终可能会得到1000 * 1000的数据帧。
#2
1
This will reproduce your example
这将重现您的示例
Value1=c("a","aa")
Value2=c("b","bb")
Value3=c("c","cc")
Value4=c("d","dd")
Value5=c("e","ee")
Value6=c("f","ff")
table1=data.frame(Value1,Value2,Value3)
table2=data.frame(Value4,Value5,Value6)
Result=cbind(table1,table2)