Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace a value NA with the value from another column in R

Tags:

replace

r

na

I want to replace the NA value in dfABy from the column A, with the value from the column B, based on the year of column year. For example, my df is:

                 >dfABy 
                 A    B   Year
                 56   75  1921
                 NA   45  1921
                 NA   77  1922
                 67   41  1923
                 NA   65  1923

The result what I will attend is:

                 > dfABy
                 A    B   Year
                 56   75  1921
                *45*  45  1921
                *77*  77  1922
                 67   41  1923
                *65*  65  1923

P.S: with the * the value replacing in column A from column B for every year

like image 671
Diego Avatar asked Dec 03 '15 16:12

Diego


People also ask

How do I replace the NA with values in R?

To replace NA with 0 in an R data frame, use is.na() function and then select all those values with NA and assign them to 0.

How do you replace a value in a column with another value in R?

replace() function in R Language is used to replace the values in the specified string vector x with indices given in list by those given in values. It takes on three parameters first is the list name, then the index at which the element needs to be replaced, and the third parameter is the replacement values.

How do I replace missing values with NA in R?

The classic way to replace NA's in R is by using the IS.NA() function. The IS.NA() function takes a vector or data frame as input and returns a logical object that indicates whether a value is missing (TRUE or VALUE). Next, you can use this logical object to create a subset of the missing values and assign them a zero.


3 Answers

Perhaps the easiest to read/understand answer in R lexicon is to use ifelse. So borrowing Richard's dataframe we could do:

df <- structure(list(A = c(56L, NA, NA, 67L, NA),
                     B = c(75L, 45L, 77L, 41L, 65L),
                     Year = c(1921L, 1921L, 1922L, 1923L, 1923L)),.Names = c("A", 
                                                                                                                            "B", "Year"), class = "data.frame", row.names = c(NA, -5L))
df$A <- ifelse(is.na(df$A), df$B, df$A)
like image 61
JHowIX Avatar answered Nov 28 '22 20:11

JHowIX


Now corrected per @Max. (original worked with initial implementation)

The new dplyr function, coalesce, can really simplify these situations.

library(dplyr)

dfABy %>% 
    mutate(A = coalesce(A,B))
like image 38
GGAnderson Avatar answered Nov 28 '22 18:11

GGAnderson


The solution provided by GGAnderson did return an error message. Using it inside mutate() however worked fine.

df <- structure(list(A = c(56L, NA, NA, 67L, NA),
                     B = c(75L, 45L, 77L, 41L, 65L),
                     Year = c(1921L, 1921L, 1922L, 1923L, 1923L)),
                .Names = c("A", "B", "Year"), 
                class = "data.frame", 
                row.names = c(NA, -5L))
df
df%>% 
  coalesce(A,B) #returns error

df %>%
mutate(A = coalesce(A,B)) #works

(I am new to Stackoverflow; My low reputation does not allow to comment on GGAnderson´s answer directly)

like image 23
Max Avatar answered Nov 28 '22 20:11

Max