问题描述
我有2个data.frame,并包含以下列。
I have 2 data.frames with the following columns.
1)A,B,C,D
2)E,F,G, H
1) A,B,C,D2) E,F,G,H
我想做的是创建一个新的data.frame,它对expand.grid(1 [,B] 2 [,F]),并保留与原始数据中col B和col F值相关的所有其他列和值。frames
What I'd like to do, is create a new data.frame, which has a row for each element of expand.grid(1[,B]2[,F]) and would keep all other columns and values associated with the values of col B and col F from the original data.frames
我目前正在使用2 for循环,由于正在处理的data.frames相当大,因此创建了相当长的运行时间。
I am currently doing this using 2 for loops and this is creating a pretty large running time since the data.frames I'm dealing with are rather large.
这里是我正在查看的屏幕截图
Here is a screenshot of what I am looking for:
> aa
A B C D
1 1 x 3 5
2 2 y 4 6
> bb
E F G H
1 7 j 9 11
2 8 k 10 12
> cc
A B C D E F G H
1 1 x 3 5 7 j 9 11
2 2 y 4 6 7 j 9 11
3 1 x 3 5 8 k 10 12
4 2 y 4 6 8 k 10 12
推荐答案
我认为,您正在寻找:
merge(aa,bb)
A B C D E F G H
1 1 x 3 5 7 j 9 11
2 2 y 4 6 7 j 9 11
3 1 x 3 5 8 k 10 12
4 2 y 4 6 8 k 10 12
这篇关于R中data.frames的expand.grid函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!