简体   繁体   English

R 问题:如何在不替换现有数据的情况下向列添加数据?

[英]R Question: How do I add data to a column without replacing existing data?

So I have a basic table with empty column 1所以我有一个空列 1 的基本表

df df

ident col1
     1   NA
     2   NA
     3   NA
     4   NA

I have two other tables:我还有另外两个表:

df1 df1

  ident col1
     2  Yes
     3  Yes

df2 df2

  ident col1
     1  No
     4  No

I am trying to add data from each df1 and df2 to the first table but when I add the second df2 data it replaces the current entires there that don't match with NA.我试图将每个 df1 和 df2 中的数据添加到第一个表中,但是当我添加第二个 df2 数据时,它会替换那里与 NA 不匹配的当前数据。 So I wan't to add df1 data into col1 and then add df2 data into col1 by matching ident numbers and without replacing values in there.所以我不想将 df1 数据添加到 col1 中,然后通过匹配标识号而不替换其中的值将 df2 数据添加到 col1 中。

Here is my code:这是我的代码:

df$col1<- df1$col1[match(df$ident, df1$ident)]

and then I do the same for df2 but it replaces df1 data...然后我对 df2 做同样的事情,但它取代了 df1 数据......

Any suggestions?有什么建议? Thanks谢谢

PS.附注。 My data is much more complicated than this, I just figured i would boil it down easier for ya.我的数据比这复杂得多,我只是想我会为你更容易地把它归结起来。

You just need to do the match the other way around你只需要反过来做匹配

df$col1[match(df1$ident, df$ident)] <- df1$col1
df$col1[match(df2$ident, df$ident)] <- df2$col1


df
#    ident col1
# 1:     1   No
# 2:     2  Yes
# 3:     3  Yes
# 4:     4   No

You can also do this with data.table update joins您也可以使用 data.table 更新连接来做到这一点

library(data.table)
setDT(df)
df[, col1 := as.character(col1)]# may not be necessary (if data is already char)

df[df1, on = .(ident), col1 := i.col1]
df[df2, on = .(ident), col1 := i.col1]


df
#    ident col1
# 1:     1   No
# 2:     2  Yes
# 3:     3  Yes
# 4:     4   No

暂无
暂无

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

相关问题 如何将观测值添加到现有数据框列? - How do I add observations to an existing data frame column? 如何在R中的现有数据集中添加一个名为“百分比”的新列。百分比是下表中提到的6项考试的平均值? - How do I add a new column named “Percentage” to the existing data set in R.Percentage is the average of the 6 exams as mentioned in the table below? 如何在 R 的数据框中添加新列并使用现有列? - How can I add a new column and use an existing column in a data frame in R? 如何在R中的数据框中添加列 - How do add a column in a data frame in R 如何从 R 中的数据集中删除问号(?) - how do I remove question mark(?) from a data set in R 如何将日历日期添加到现有数据表中,以便在R中占一整个月? - How do I add calendar dates to an existing data table so that an entire month is accounted for in R? 如何将列添加到使用包含现有列变量的数学方程的数据框中? - How Do I add column to data frame that uses a mathematical equation with variables from the existing columns? 如何分解重复的数据,并在数据框中添加一列,求出r中的溶解量之和? - How do I dissolve repeating data and add a column to my data frame summing the quantity dissolved in r? 如何根据另一列中的部分字符串向 R 中的数据框添加一列? - How do I add a column to a data frame in R based on a partial string in another column? 如何在 data.table 中添加一列并返回多列而不修改基础数据? - How do I add a column to a data.table and return multiple columns without modifying underlying data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM