Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

R data frame merge by multiple columns in df.x

$
0
0

Is it possible to merge by multiple column in df_a

For example if I have df_a and df_b, where my df_a is looking like

SKS1 SKS2
UXCC77  NA
UXCC00  NA
UXCC00  UXCD00
UXCD00  NA
UXCD60  NA
UXME10  NA

and df_b is have the descriptions for my SKS codes.

SKS desc
UXCC77  Left Leg
UXCC00  Right Leg
UXCD00  Right Arm
UXCD60  Left Arm
UXME10  Head

and etc.

I wish to do something like

new_df <- merge(x = df_a, y = df_b, by.x = paste0("SKS", 1:ncol(df_a)), by.y = "SKS", all.x = TRUE)

So that my output is

SKS1 SKS2 Desc1 Desc2
UXCC77  NA Left Leg NA
UXCC00  NA Right Leg NA
UXCC00  UXCD00 Right Leg   Right Arm
UXCD00  NA Right Arm NA
UXCD60  NA Left Arm NA
UXME10  NA Head NA

or with only one desc where a "/" is seperating the descriptions.

So that my output is

SKS1 SKS2 Desc1
UXCC77  NA "Left Leg"
UXCC00  NA "Right Leg"
UXCC00  UXCD00 "Right Leg"/"Right Arm"
UXCD00  NA "Right Arm"
UXCD60  NA "Left Arm"
UXME10  NA "Head"

dput of df_a

structure(list(SKS_koder_samlet = structure(c(3L, 1L, 2L, 4L, 
5L, 6L), .Label = c("UXCC00", "UXCC00/UXCD00", "UXCC77", "UXCD00", 
"UXCD60", "UXME10"), class = "factor"), SKS1 = structure(c(2L, 
1L, 1L, 3L, 4L, 5L), .Label = c("UXCC00", "UXCC77", "UXCD00", 
"UXCD60", "UXME10"), class = "factor"), SKS2 = structure(c(NA, 
NA, 3L, NA, NA, NA), .Label = c("UXCC00", "UXCC77", "UXCD00", 
"UXCD60", "UXME10"), class = "factor")), class = "data.frame", row.names = c(NA, 
-6L))

Viewing all articles
Browse latest Browse all 201839

Trending Articles