用ID替换另一个数据框中的值 [英] Replace values from another dataframe by IDs

查看:86
本文介绍了用ID替换另一个数据框中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据帧::

as1 <- data.frame(ID = c(1,2,3,4,5,6),
                  pID = c(21,22,23,24,25,26),
                  Values = c(435,33,45,NA, NA,12))
as2 <- data.frame(ID = c(4,5),
                  pid = c(24,25),
                  Values = c(544, 676))

我需要通过匹配ID和pID将as1中的NA值替换为as2中的NA值

I need to replace the NA values in as1 with those in as2 by matching ID and pID

我需要获取结果数据框为:

I need to get the result data frame as:

  resultdf
    ID pID Values
    1  1  21    435
    2  2  22     33
    3  3  23     45
    4  4  24    544
    5  5  25    676
    6  6  26     12

我尝试做子集,然后 na.omit(),然后 rbind ...但是我丢失了索引。

I tried doing subset and then na.omit() and then rbinding... but I'm losing the index.

推荐答案

这是两个基本的R解决方案。

Here are two base R solutions.

首先,在 ID中使用 match 来选择 在as1中填写值的元素:

First, using match in "ID" to select the elements of "Value" in as1 to fill in:

as1$Values[match(as2$ID, as1$ID)] <- as2$Values

as1
  ID pID Values
1  1  21    435
2  2  22     33
3  3  23     45
4  4  24    544
5  5  25    676
6  6  26     12

仅当ID是两个数据集的真实ID(即pid是不相关的)时,这才起作用。其次,在还需要pid的情况下,可以使用 merge 然后折叠两个值列,如下所示:

This only works if ID is the true ID for both data sets (that is, pid is "irrelevant"). Second, in the case that pid is also needed, you could use merge and then "collapse" the two values columns as follows:

df <- merge(as1, as2, by.x=c("ID", "pID"), by.y=c("ID", "pid"), all=TRUE)

这将产生一个包含两个值列的四列数据帧。用 ifelse 将这些折叠为一个列:

This produces a four column data frame with two values columns. Collapse these into a single column with ifelse:

df <- cbind(df[c(1,2)], "Values"=with(df, ifelse(is.na(Values.y), Values.x, Values.y)))

df
  ID pID Values
1  1  21    435
2  2  22     33
3  3  23     45
4  4  24    544
5  5  25    676
6  6  26     12

这篇关于用ID替换另一个数据框中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆