我有兩個資料框,如下所示:
structure(list(ID = c("a", "a", "c", "d", "a", "f"), a1 = c("2001",
"2000", "2000", "1998", "2006", "2002"), value = c("100", NA,
"200", NA, "400", "500")), class = "data.frame", row.names = c(NA,
-6L))
ID2 <- c("x","a","c","d","n","f","g","m")
a2 <- c(2000,2000,2000,2000,2000,2000,2000,2000)
value2 <- c(100, 1000, 20000, 1000, 400,500,1,1)
ID a1 value
1 a 2001 100
2 a 2000 <NA>
3 c 2000 200
4 d 1998 <NA>
5 a 2006 400
6 f 2002 500
ID2 a2 value2
1 x 2000 100
2 a 2000 1000
3 c 2000 20000
4 d 2000 1000
5 n 2000 400
6 f 2000 500
7 g 2000 1
8 m 2000 1
我想要做什么:我想用資料框2的value2列中的相應值替換資料框1的列值中的缺失值。年份和ID應該匹配。我嘗試使用以下代碼;但是,這給了我錯誤的結果left_join
r <- left_join(data, data2, by=c("ID"="ID2","a1"="a2"))
有什么建議嗎?
預期結果
ID a1 value
1 a 2001 100
2 a 2000 **1000**
3 c 2000 200
4 d 1998 <NA>
5 a 2006 400
6 f 2002 500
uj5u.com熱心網友回復:
這是一種tidyverse方法。
請注意,由于您的a1and valuefrom 資料框 1 是character,我還將資料框 2 的相應列更改為character。
您需要做的是將left_join它們放在一起,并coalesce與.valuevalue2
library(tidyverse)
ID2 <- c("x","a","c","d","n","f","g","m")
a2 <- c("2000","2000","2000","2000","2000","2000","2000","2000")
value2 <- c("100", "1000", "20000", "1000", "400","500","1","1")
df2 <- data.frame(ID2, a2, value2)
left_join(df1, df2, by = c("ID" = "ID2", "a1" = "a2")) %>%
transmute(ID = ID, a1 = a1, value = coalesce(value, value2))
ID a1 value
1 a 2001 100
2 a 2000 1000
3 c 2000 200
4 d 1998 <NA>
5 a 2006 400
6 f 2002 500
uj5u.com熱心網友回復:
僅使用基礎 R。
merge(df1, df2, by.x=c('ID', 'a1'), by.y=c('ID2', 'a2'), all.x=TRUE) |>
transform(value=ifelse(is.na(value), value2, value), value2=NULL)
# ID a1 value
# 1 a 2000 1000
# 2 a 2001 100
# 3 a 2006 400
# 4 c 2000 200
# 5 d 1998 NA
# 6 f 2002 500
資料:
df1 <- structure(list(ID = c("a", "a", "c", "d", "a", "f"), a1 = c(2001L,
2000L, 2000L, 1998L, 2006L, 2002L), value = c(100L, NA, 200L,
NA, 400L, 500L)), class = "data.frame", row.names = c("1", "2",
"3", "4", "5", "6"))
df2 <- structure(list(ID2 = c("x", "a", "c", "d", "n", "f", "g", "m"
), a2 = c(2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L
), value2 = c(100L, 1000L, 20000L, 1000L, 400L, 500L, 1L, 1L)), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/428235.html
