Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: replacing NAs in a data.frame with values in the same position in another dataframe

Tags:

I have a dataframe with some NA values:

dfa <- data.frame(a=c(1,NA,3,4,5,NA),b=c(1,5,NA,NA,8,9),c=c(7,NA,NA,NA,2,NA)) dfa 

I would like to replace the NAs with values in the same position in another dataframe:

dfrepair <- data.frame(a=c(2:7),b=c(6:1),c=c(8:3)) dfrepair 

I tried:

dfa1 <- dfa  dfa1 <- ifelse(dfa == NA, dfrepair, dfa) dfa1 

but this did not work.

like image 758
adam.888 Avatar asked Dec 15 '16 23:12

adam.888


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 I replace NAs with blanks in R?

How to replace NA (missing values) with blank space or an empty string in an R dataframe? You can replace NA values with blank space on columns of R dataframe (data. frame) by using is.na() , replace() methods.

Why is my data showing as Na in R?

In R (or R Studio), NA stands for Not Available. Each cell of your data that displays NA is a missing value. Not available values are sometimes enclosed by < and >, i.e. <NA>. That happens when the vector or column that contains the NA is a factor.

How to replace Na values in columns of an R data frame?

How to replace NA values in columns of an R data frame form the mean of that column? In the whole world, the first step people teach to impute missing values is replacing them with the relevant mean. That means if we have a column which has some missing values then replace it with the mean of the remaining values.

How to replace a value in a data frame with another?

You can use the following syntax to replace one of several values in a data frame with a new value: df [df == 'Old Value 1' | df == 'Old Value 2'] <- 'New value' And you can use the following syntax to replace a particular value in a specific column of a data frame with a new value: df ['column1'] [df ['column1'] == 'Old Value'] <- 'New value'

How to replace Na with strings in R?

How to Replace NAs with Strings in R (With Examples) You can use the replace_na () function from the tidyr package to replace NAs with specific strings in a column of a data frame in R: #replace NA values in column x with "missing" df$x %>% replace_na('none')

What is the difference between replace and replace_Na in Python?

replace_na (data, replace, ...) data: It is a data frame or Vector. replace: If the data is a Vector, the replace takes a single value. If the data is a data frame, the replace takes a list of values, with one value for each column that has NA values to be replaced. If the input data is a data frame, the replace_na () method returns a data frame.


2 Answers

You can do:

dfa <- data.frame(a=c(1,NA,3,4,5,NA),b=c(1,5,NA,NA,8,9),c=c(7,NA,NA,NA,2,NA)) dfrepair <- data.frame(a=c(2:7),b=c(6:1),c=c(8:3)) dfa[is.na(dfa)] <- dfrepair[is.na(dfa)] dfa    a b c 1 1 1 7 2 3 5 7 3 3 4 6 4 4 3 5 5 5 8 2 6 7 9 3 
like image 130
gfgm Avatar answered Sep 21 '22 20:09

gfgm


In the tidyverse, you can use purrr::map2_df, which is a strictly bivariate version of mapply that simplifies to a data.frame, and dplyr::coalesce, which replaces NA values in its first argument with the corresponding ones in the second.

library(tidyverse)  dfrepair %>%      mutate_all(as.numeric) %>%    # coalesce is strict about types     map2_df(dfa, ., coalesce)  ## # A tibble: 6 × 3 ##       a     b     c ##   <dbl> <dbl> <dbl> ## 1     1     1     7 ## 2     3     5     7 ## 3     3     4     6 ## 4     4     3     5 ## 5     5     8     2 ## 6     7     9     3 
like image 45
alistaire Avatar answered Sep 21 '22 20:09

alistaire