How do find values that occur on the same date between two different columns in the same dataframe. Delete the values occurring in A01_CD columns and replace it with NA. And in cases where A01_CD has values, it should be moved to A01 and NA should be placed in A01. I have been trying to used duplicate () and Unique () and ifelse, but I am failing. Kindly assist.

My dataframe
Date A01 A01_CD
1 1966/05/07 4.870000 4.870
2 1966/05/08 4.918333 NA
3 1966/05/09 4.892000 4.860
4 1966/05/10 4.858917 NA
5 1966/05/11 4.842000 NA
211 1967/03/18 NA 5.95

Desired Outcome
Date A01 A01_CD
1 1966/05/07 4.870000 NA
2 1966/05/08 4.918333 NA
3 1966/05/09 4.892000 NA
4 1966/05/10 4.858917 NA
5 1966/05/11 4.842000 NA
211 1967/03/18 5.95 NA

Is this what you are looking for?

library(dplyr, warn.conflicts = FALSE)
DF <- data.frame(A01 = c(4.87, 4.91, 4.89, 4.85, 4.84, NA),
              A01_CD = c(4.87, NA, 4.86, NA, NA, 5.95))
DF
#>    A01 A01_CD
#> 1 4.87   4.87
#> 2 4.91     NA
#> 3 4.89   4.86
#> 4 4.85     NA
#> 5 4.84     NA
#> 6   NA   5.95
DF <- DF %>% mutate(A01 = ifelse(is.na(A01), A01_CD, A01),
                    A01_CD =NA)
DF
#>    A01 A01_CD
#> 1 4.87     NA
#> 2 4.91     NA
#> 3 4.89     NA
#> 4 4.85     NA
#> 5 4.84     NA
#> 6 5.95     NA

Created on 2020-09-19 by the reprex package (v0.3.0)

1 Like

Yes, Thank you :grinning:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.