[英]Assign new column based on a value chosen from an id in another dataframe
I have a df with 100k+ obs and 12 cols. 我有一个100k + obs和12 cols的df。 One of those cols is some kind of an id I need to use to make another column based in another df. 这些列中的一个是某种ID,我需要使用它来创建基于另一个df的另一列。 This other df has only 50 obs and one col is the id and the value I need to copy to the first df. 另一个df只有50个obs,一个col是id和我需要复制到第一个df的值。
I am not able to code this. 我无法对此进行编码。 Here is a partial df (both) I am showing only the relevant cols for this question 这是部分df(均为),我仅显示此问题的相关列
DF1 (100k+ obs) DF1(100k + obs)
id
010100
010100
010100
010100
010100
010100
010200
010200
010200
010201
010201
010201
010201
010201
010201
010201
010300
010300
010300
010300
010300
010400
010400
010400
010500
010500
010501
010501
010501
010600
010600
010600
010600
Here is the second df with the values and id 这是带有值和id的第二个df
id val
010100 1
010200 2
010201 2
010300 3
010400 4
010500 5
010501 6
010600 7
What I need is to have val in a new column in df depending on the id of both df as follows: 我需要根据两个df的ID在df的新列中添加val,如下所示:
id New
010100 1
010100 1
010100 1
010100 1
010100 1
010100 1
010200 2
010200 2
010200 2
010201 2
010201 2
010201 2
010201 2
010201 2
010201 2
010201 2
010300 3
010300 3
010300 3
010300 3
010300 3
010400 4
010400 4
010400 4
010500 5
010500 5
010501 6
010501 6
010501 6
010600 7
010600 7
010600 7
010600 7
Any idea is appreciated. 任何想法表示赞赏。 Thanks for your time. 谢谢你的时间。
Regards 问候
merge
is what you want, or alternatively you may notice some speed benefits by using data.table
package: merge
是您想要的,或者使用data.table
包,您可能会注意到一些速度上的好处:
df1 <- data.frame(id = 1:3)
df2 <- data.frame(id = rep(1:3, each = 2), val = rnorm(6))
> merge(df1, df2)
id val
1 1 0.9462113
2 1 -1.7835754
3 2 -1.1604525
4 2 0.2498844
5 3 -1.5187111
6 3 0.5921281
library(data.table)
dt1 <- data.table(df1, key = "id")
dt2 <- data.table(df2, key = "id")
> dt1[dt2]
id val
[1,] 1 0.9462113
[2,] 1 -1.7835754
[3,] 2 -1.1604525
[4,] 2 0.2498844
[5,] 3 -1.5187111
[6,] 3 0.5921281
See the help page for ?merge
for details on the types of joins available, matching columns, etc. The data.table FAQ is probably the best place to learn the nuances of that package: http://datatable.r-forge.r-project.org/datatable-faq.pdf 有关可用连接类型,匹配列等的详细信息,请参见帮助页面上的?merge
”。data.table FAQ可能是了解该程序包细微差别的最佳位置: http://datatable.r-forge.r -project.org/datatable-faq.pdf
You might try something like this: 您可以尝试如下操作:
df3 <- merge(df1, df2, by="id", all = TRUE)
You need to set all = TRUE
or only df2 rows will exist in df3. 您需要将all = TRUE
设置all = TRUE
否则df3中将仅存在df2行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.