温馨提示:本文翻译自stackoverflow.com,查看原文请点击:r - How to join with ColumnA = (ColumnB
r

r - 如何加入ColumnA =(ColumnB

发布于 2020-03-27 10:45:18

我正在尝试加入两个数据框。连接的条件不是ColumnA = ColumnB *,而是ColumnA = ColumnB * Function。通过功能合并,我看不到如何处理

有个例子

df1 <- data.frame(ID=c(5,4,3,2), CASE=c("A","B","C","D"))
df2 <- data.frame(ID=c(6,5,4,3), RESULT=c("ResultA","ResultB","ResultC","ResultD"))

我想将df1和df2与df1 $ ID = df2 $ ID-1之类的东西结合在一起,以得到结果:

df_result<- data.frame(ID_df1=c(5,4,3,2), CASE=c("A","B","C","D"), RESULT=c("Result5","Result4","Result3","Result2"))

我想删除联接中的引号,但是它不起作用:

df_result <- merge ( x = df1, y = df2, by.x = ID , by.y = ID - 1 , all.x = TRUE)

有人可以帮我吗?:)

谢谢 !

查看更多

查看更多

提问者
Karibuu
被浏览
91
Maurits Evers 2019-07-03 21:12

一个tidyverse解决方案来重现您的预计产出将是

library(tidyverse)
left_join(df1, df2 %>% mutate(ID = ID - 1)) %>%
    mutate(RESULT = str_replace(RESULT, "^(.+)[A-Z]$", paste0("\\1", ID)))
#Joining, by = "ID"
#  ID CASE  RESULT
#1  5    A Result5
#2  4    B Result4
#3  3    C Result3
#4  2    D Result2

解释:如果你只是想通过合并IDID - 1简单

left_join(df1, df2 %>% mutate(ID = ID - 1))
#  ID CASE  RESULT
#1  5    A ResultA
#2  4    B ResultB
#3  3    C ResultC
#4  2    D ResultD

足够了。附加项mutate负责RESULT根据您的预期输出进行重命名


否则基本R选项将从

merge(df1, transform(df2, ID = ID - 1), by = "ID")
#  ID CASE  RESULT
#1  2    D ResultD
#2  3    C ResultC
#3  4    B ResultB
#4  5    A ResultA

包括重命名 RESULT

transform(
    merge(df1, transform(df2, ID = ID - 1), by = "ID"),
    RESULT = paste0(substr(RESULT, 1, nchar(as.character(RESULT)) - 1), ID))
#  ID CASE  RESULT
#1  2    D Result2
#2  3    C Result3
#3  4    B Result4
#4  5    A Result5

再现您的预期输出(行顺序略有不同)。

发布
问题

分享
好友

手机
浏览

扫码手机浏览