[英]Assign values in one vector based on matching values in two other vectors in R
I have dataset1 with an id column and a number column.我的 dataset1 有一个 id 列和一个 number 列。 The id number appears multiple times, it can be 5 times or 60 times, or something else.
id号出现多次,可以是5次,也可以是60次,或者别的什么。
id number
2 NA
4 NA
9 ...
1
2
2
3
5
5
5
12
I have dataset2 where each id only appears once and then a value for the number我有 dataset2,其中每个 id 只出现一次,然后是数字的值
id number
1 12
2 25
3 33
4 121
5 35
9 1500
There are id's in dataset1 tat do not exist in dataset2. dataset1 中有 id,但在 dataset2 中不存在。
I want to assign the number of dataset2 to the empty number column in dataset1 for the right id's.我想将 dataset2 的数量分配给 dataset1 中的空数字列以获得正确的 ID。 I tried a few things, but it always gives me an error that the number of observations I want to replace doesn´t match with the input of observations.
我尝试了一些东西,但它总是给我一个错误,即我想要替换的观察数量与观察的输入不匹配。 I know this is the case because my dataset2 has less observations than dataset1.
我知道是这种情况,因为我的数据集 2 的观测值少于数据集 1。 Some things I tried:
我尝试过的一些事情:
dataset1[na.omit(match(dataset1$id, dataset2$id)), ]$number <- data_new[dataset2$id %in% dataset1$id, ]$number
dataset1$number <- ifelse(dataset2$id %in% dataset1$id, dataset2$number, NA)
I would appreciate any help!我将不胜感激任何帮助! Thanks!!
谢谢!!
Assuming that the "number" column in your dataset1 only contains NAs, the simplest solution would be to tidy up your dataset1 to a vector of ids and left_join dataset2:假设您的 dataset1 中的“数字”列仅包含 NA,最简单的解决方案是将您的 dataset1 整理为 ids 和 left_join dataset2 的向量:
library(dplyr)
tidy <- dataset1 %>%
group_by(id) %>%
summarise()
merge <- left_join(tidy, dataset2, by = "id")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.