Saahil
Saahil

Reputation: 59

For a column in R dataframe, even if one value is not NA, then replace all NA's with that value?

I have a dataframe in R with more than 20 columns and a few million rows. For a given id, the dataframe can have about 100 observations. The way I have created this dataframe is by merging two data frames. So for a given id, for a column say date, even if one of the values is not NA, then I want to replace all NA values in the date column for that id with the value I found. This column is generated by me in some other code so it can have either a single unique value or NA and nothing else.

The closest similar question I could find was this.

This is the best solution that I could come up with, but I'm sure there is a more elegant way of doing this:

merged_df <- merged_df %>%
    filter(isAMI == 1) %>%
    group_by(accountnumber) %>% 
    mutate(amiStartDate = if_else(id == 1,
                                  amiStartDate[!is.na(amiStartDate)][1],
                                  amiStartDate))

The basic, idea is I am finding the right places where I would want to make the change and changing all entries corresponding to NA values.

Upvotes: 0

Views: 74

Answers (1)

Daniel D. Sjoberg
Daniel D. Sjoberg

Reputation: 11804

Would something like this work for you?

library(magrittr)
dta = data.frame(
  id = c(1,1,1,1,2,2,2,2),
  value = c(rep(NA, 3), 2, 1, NA, NA, NA)
) 

dta %>%
  dplyr::group_by(id) %>%
  dplyr::mutate(
    value = max(value, na.rm = TRUE)
  )
#> # A tibble: 8 x 2
#> # Groups:   id [2]
#>      id value
#>   <dbl> <dbl>
#> 1     1     2
#> 2     1     2
#> 3     1     2
#> 4     1     2
#> 5     2     1
#> 6     2     1
#> 7     2     1
#> 8     2     1

Upvotes: 1

Related Questions