1

Hello I have 2 dataframe

The first

df1

target Name
SP1.1  Lupus
SP2.2  Canis
SP3.3  Canis

and the second where I have NA values in the Name columns that I need to fill according to df1$Name and df1$target matches between the two df df2

target Name COLn
SP1.1  <NA>  9
SP2.2  Canis 32
SP3.3  <NA>  78
SP4    Canis 7

The expected result would be

df2

target Name   COLn
SP1.1  Lupus  9
SP2.2  Canis  32
SP3.3  Canis  78
SP4    Canis  7

doessomeone have an idea?

2 Answers 2

1

You can join df1 and df2 by target and select first non-NA value using coalesce for Name column.

library(dplyr)
df1 %>%
  right_join(df2, by = 'target') %>%
  mutate(Name = coalesce(Name.x, Name.y)) %>%
  select(names(df2))

#  target  Name COLn
#1  SP1.1 Lupus    9
#2  SP2.2 Canis   32
#3  SP3.3 Canis   78
#4    SP4 Canis    7

In base R, you can do :

transform(merge(df1, df2, all.y = TRUE, by = 'target'), 
          Name = ifelse(is.na(Name.x), Name.y, Name.x))[names(df2)]
Sign up to request clarification or add additional context in comments.

Comments

1

If target uniquely identifies rows, you can use dplyr::rows_patch().

library(dplyr)

df2 %>%
  rows_patch(df1, by = "target")

  target  Name COLn
1  SP1.1 Lupus    9
2  SP2.2 Canis   32
3  SP3.3 Canis   78
4    SP4 Canis    7

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.