Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
匹配R中某个表中的数据集_R_Join_Matching - Fatal编程技术网

匹配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