简体   繁体   English

根据从另一个数据框中的ID中选择的值分配新列

[英]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.

相关问题 如何根据 R 中不同列的值在数据框的列中分配新值? - How to assign a new value in a column of a dataframe based on a value from a different column in R? 如何将 1 个数据框中的值分配给另一个数据框中的新列 - how to assign values from 1 dataframe to a new column in another dataframe 根据 R 中 dataframe 的另一列的相等值,在新列(在第一个数据帧中)中添加值(来自第二个数据帧) - Add value (from 2nd dataframe) in new column (in 1st dataframe) based on equality value of another column from both dataframe in R 根据另一个数据框中的列在一个数据框中创建新列 - Creating new column in one dataframe based on column from another dataframe 根据与另一个数据框的值匹配在数据框上创建新列 - Create a new column on a dataframe based on value match with another dataframe 如何根据另一列的相等性对列的值求和并创建新的 dataframe - How to sum value of a column based on equality from another column and create a new dataframe 根据行 ID 将“文件名”列分配给 dataframe - Assign 'filename' column to dataframe based on row ID 根据另一个 dataframe 的多个列向 dataframe 添加新列 - Add a new column to a dataframe based on multiple columns from another dataframe 根据条件将值分配给另一列中的一列 - Assign value to a column from another column based on condition 基于来自 dataframe 的另一列,使用 rnorm function 创建新列 - Create new column based on another column from dataframe with rnorm function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM