问题描述
我到处都是stackoverflow,找不到我想要的东西,因此,如果这是重复的帖子,对不起,非常感谢您的链接!
I've looked around stackoverflow and couldn't find what i was looking for, so if this is a duplicate post, sorry AND I'd greatly appreciate the link!
我有两个数据框:CarDF和重复的CarDF
I have two data frames: CarDF and duplicateCarDF
ID <- c(1,2,3,4,5,6,7,8)
car <- c("acura", "audi", "benz", "benz", "bmw", "toyota", "toyota", "jeep")
year <- c(2001, 2002, '2004', '2016','1999', '2017', '2017',2005)
CarDF <- data.frame(ID, car, year)
ID2 <-c(4,7)
car2 <- c("benz2", "toyota2")
year2 <- c(2016, 2017)
duplicateCarDF <- data.frame(ID = ID2, car = car2, year = year2)
我的目标是使用基于ID的重复CarDF中的更新名称来更新CarDF中的汽车。
My goal is to update the cars in CarDF with the updated names in duplicateCarDF based on the IDs.
我尝试了以下操作...
I've tried the following...
CarDF$car <- ifelse(duplicateCarDF$ID %in% CarDF$ID, duplicateCarDF$car, CarDF$car )
但它将汽车名称交替更改为benz2和Toyota2。我只想更新ID 4和7的汽车。
but it changes the car names to benz2 and toyota2 alternating. I just want to update the car for ID 4 and 7.
任何帮助将不胜感激!
推荐答案
使用 dplyr 动词,我们可以通过 ID
left_join
然后有条件地替换 car
根据新值是否丢失。
Using dplyr verbs we can left_join
by ID
and then conditionally replace car
based on whether or not the new value is missing.
library(dplyr)
CarDF %>%
left_join(
duplicateCarDF %>% # note: the year column doesn't add any
select(ID, new_car = car), # value here unless you have duplicated ID values
by = "ID"
) %>%
mutate(
car = if_else(
is.na(new_car),
as.character(car), # note: I'm coercing these to character because
as.character(new_car) # we've joined two df with different levels
)
) %>%
select(-new_car)
# ID car year
# 1 1 acura 2001
# 2 2 audi 2002
# 3 3 benz 2004
# 4 4 benz2 2016
# 5 5 bmw 1999
# 6 6 toyota 2017
# 7 7 toyota2 2017
# 8 8 jeep 2005
这篇关于根据R中的另一个数据帧更新列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!