How to replace NA with set of values

Related searches

I have the following data frame:

library(dplyr)
library(tibble)


df <- tibble(
  source = c("a", "b", "c", "d", "e"),
  score = c(10, 5, NA, 3, NA ) ) 


df

It looks like this:

# A tibble: 5 x 2
  source score
  <chr>  <dbl>
1 a         10 . # current max value
2 b          5
3 c         NA
4 d          3
5 e         NA

What I want to do is to replace NA in score column with values ranging for existing max + n onwards. Where n range from 1 to total number of rows of the df

Resulting in this (hand-coded) :

  source score
  a         10
  b          5
  c         11 # obtained from 10 + 1
  d          3
  e         12 #  obtained from 10 + 2

How can I achieve that?


Another option :

transform(df, score = pmin(max(score, na.rm = TRUE) + 
                      cumsum(is.na(score)), score, na.rm = TRUE))

#  source score
#1      a    10
#2      b     5
#3      c    11
#4      d     3
#5      e    12

If you want to do this in dplyr

library(dplyr)
df %>% mutate(score = pmin(max(score, na.rm = TRUE) + 
                      cumsum(is.na(score)), score, na.rm = TRUE))

How do I replace NA values with zeros in an R dataframe?, When dealing with missing values, you might want to replace values with a a set of (annoying) repeating strings like various spellings of “NA”,� Step 3) Replace the NA Values The verb mutate from the dplyr library is useful in creating a new variable. We don't necessarily want to change the original column so we can create a new variable without the NA. mutate is easy to use, we just choose a variable name and define how to create this variable.


A base R solution

df$score[is.na(df$score)] <- seq(which(is.na(df$score))) + max(df$score,na.rm = TRUE)

such that

> df
# A tibble: 5 x 2
  source score
  <chr>  <dbl>
1 a         10
2 b          5
3 c         11
4 d          3
5 e         12

Replacing values with NA, You need to add "None" to the factor level and refactor the column DF$col. I added an example script using the iris dataset. df <- iris # set 20 Species to NA� Value to replace with NA. Value. A modified version of x that replaces any values that are equal to y with NA. See also. coalesce() to replace missing values with a


Here is a dplyr approach,

df %>% 
 mutate(score = replace(score, 
                       is.na(score), 
                       (max(score, na.rm = TRUE) + (cumsum(is.na(score))))[is.na(score)])
                       )

which gives,

# A tibble: 5 x 2
  source score
  <chr>  <dbl>
1 a         10
2 b          5
3 c         11
4 d          3
5 e         12

How to replace NA values with another value in factors in R?, data. A data frame or vector. replace. If data is a data frame, replace takes a list of values, with one value for each column that has NA values to be replaced. replace: If data is a data frame, replace takes a list of values, with one value for each column that has NA values to be replaced. If data is a vector, replace takes a single value. This single value replaces all of the NA values in the vector. Additional arguments for methods. Currently unused.


With dplyr:

library(dplyr)

df %>%
  mutate_at("score", ~ ifelse(is.na(.), max(., na.rm = TRUE) + cumsum(is.na(.)), .))

Result:

# A tibble: 5 x 2
  source score
  <chr>  <dbl>
1 a         10
2 b          5
3 c         11
4 d          3
5 e         12

Replace NAs with specified values — replace_na • tidyr, This is just a tutorial showing how you can replace NA's in a data frame with other values(such Duration: 5:32 Posted: Feb 18, 2015 Replace the NA values with the mean of the values In the data analysis process, sometimes eliminating the entire row or a column just for the sake of one or more NA values is not a good idea. You simply cannot eliminate most of the values, as it may result in bad accuracy and results.


A dplyr solution.

df %>%
  mutate(na_count = cumsum(is.na(score)),
         score = ifelse(is.na(score), max(score, na.rm = TRUE) + na_count, score)) %>%
  select(-na_count)
## A tibble: 5 x 2
#  source score
#  <chr>  <dbl>
#1 a         10
#2 b          5
#3 c         11
#4 d          3
#5 e         12

Replacing NA values with different values in Data Frames in R , Replace missing values Arguments data. A data frame or vector. replace. If data is a data frame, a named list giving the value to replace NA with for each column. If data is a vector, a single value used for replacement.


Replacing values with NA Nicholas Tierney 2020-09-02. When dealing with missing values, you might want to replace values with a missing values (NA).This is useful in cases when you know the origin of the data and can be certain which values should be missing.


The dplyr hybridized options are now around 30% faster than the Base R subset reassigns. On a 100M datapoint dataframe mutate_all(~replace(., is.na(.), 0)) runs a half a second faster than the base R d[is.na(d)] <- 0 option.


Hence I want replace every value in the given column with " Stack Exchange Network Stack Exchange network consists of 176 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.