匹配R中某个表中的数据集
我希望这个过程 原来的一个匹配R中某个表中的数据集,r,join,matching,R,Join,Matching,我希望这个过程 原来的一个 A B LN1 1 LN2 2 LN3 3 桌子 我想要什么 A B LN1 1 LN2 2 LN3 3 LN4 1 我想粘贴原始的一个,以引用像LN1这样的带有属性的表(LN1-1;LN4-1导致了该表)。。。如何生成代码?尝试从dplyr包中加入函数! df1 = read.table(text = " A B LN1 1 LN2 2 LN3 3 ", header=T, stringsAsFac
A B
LN1 1
LN2 2
LN3 3
桌子
我想要什么
A B
LN1 1
LN2 2
LN3 3
LN4 1
我想粘贴原始的一个,以引用像LN1这样的带有属性的表(LN1-1;LN4-1导致了该表)。。。如何生成代码?尝试从
dplyr
包中加入函数!
df1 = read.table(text = "
A B
LN1 1
LN2 2
LN3 3
", header=T, stringsAsFactors=F)
df2 = data.frame(A = c("LN1","LN4"), stringsAsFactors = F)
library(dplyr)
left_join(df2, df1, by="A") %>% # join datasets
mutate(B = unique(B[!is.na(B)])) %>% # replace NAs in column B with the unique non-NA value
bind_rows(df1) %>% # bind original dataset
distinct() # keep distinct rows
# A B
# 1 LN1 1
# 2 LN4 1
# 3 LN2 2
# 4 LN3 3
df1 = read.table(text = "
A B
LN1 1
LN2 2
LN3 3
", header=T, stringsAsFactors=F)
df2 = data.frame(A = c("LN1","LN4"), stringsAsFactors = F)
library(dplyr)
left_join(df2, df1, by="A") %>% # join datasets
mutate(B = unique(B[!is.na(B)])) %>% # replace NAs in column B with the unique non-NA value
bind_rows(df1) %>% # bind original dataset
distinct() # keep distinct rows
# A B
# 1 LN1 1
# 2 LN4 1
# 3 LN2 2
# 4 LN3 3