【问题标题】:Matching a data-set from a certain table in R匹配 R 中某个表中的数据集
【发布时间】:2018-02-03 15:01:04
【问题描述】:

我想要这个过程...

原件;

A     B
LN1   1
LN2   2
LN3   3

表格

A   
LN1   
LN4  

我想要什么

A     B
LN1   1
LN2   2
LN3   3
LN4   1

我想粘贴原始的,以引用具有属性(LN1-1;LN4-1 导致该表)的 LN1 之类的表...我如何制作代码?

【问题讨论】:

  • 试试 dplyr 包中的连接函数!

标签: r join matching


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2017-12-15
    • 1970-01-01
    相关资源
    最近更新 更多