R: replace NA with item from vector
ifelse
is your friend.
Using Dirk's dataset
df <- within(df, X <- ifelse(is.na(X), Y, X))
Replace NA's in a dataframe with a vector of numbers in r
We need to replace the specific column
mydata$Freq2[is.na(mydata$Freq2)] <- L8Replace
mydata
# Freq2 myDate numDays
#2015-04-16 0.42 2015-04-16 0 days
#2015-04-21 0.60 2015-04-21 5 days
#2015-04-26 0.43 2015-04-26 10 days
#7 0.56 2015-07-11 86 days
#8 0.57 2015-07-27 102 days
#9 0.87 2015-08-12 118 days
#10 0.81 2015-08-28 134 days
#11 0.00 2015-09-13 150 days
Also, the length
of 'L8Replace' should match the number of NA in 'Freq2'
data
L8Replace <- c(0.56, 0.57, 0.87,0.81, 0)
Replace NA using a vector of column names
columns
value should be string, you can then use is.na
as -
columns<-c("col1","col2")
data[columns][is.na(data[columns])] <- 0
data
# col1 col2 col3 col4
#1 9 9 NA 9
#2 0 5 3 1
#3 25 25 25 NA
#4 26 26 26 26
#5 0 0 NA NA
#6 51 51 51 51
Or using tidyverse
-
library(dplyr)
library(tidyr)
data <- data %>% mutate(across(all_of(columns), replace_na, 0))
Replace NA's with a vector
From what I read, you want to replace the NA
s in each column of a matrix with a corresponding value of a vector.
Lets say this is your data set and your replacement vector
set.seed(1)
(m <- matrix(sample(c(NA, 1), 25, replace = TRUE), ncol = 5))
# [,1] [,2] [,3] [,4] [,5]
# [1,] NA 1 NA NA 1
# [2,] NA 1 NA 1 NA
# [3,] 1 1 1 1 1
# [4,] 1 1 NA NA NA
# [5,] NA NA 1 1 NA
(vec <- sample(5))
## [1] 2 1 5 3 4
Here's a quite simple vectorized way to replace the NA
s with corresponding values
indx <- is.na(m)
m[indx] <- vec[col(m)][indx]
# [,1] [,2] [,3] [,4] [,5]
# [1,] 2 1 5 3 1
# [2,] 2 1 5 1 4
# [3,] 1 1 1 1 1
# [4,] 1 1 5 3 4
# [5,] 2 1 1 1 4
This is basically takes the advantage is.na
returning a logical matrix that can be used as a subsetting index and the col
function which converts vec
to a matrix.
Replace NA values in a data frame column with values with elements from a vector
We can do an assignment by replicating the vector
i1 <- is.na(df)
df[i1] <- v1[col(df)][i1]
Or we can use
v1[col(df)]*is.na(df) + replace(df, is.na(df), 0)
Or we can use Map
to replace
the corresponding columns NAs with the vector
elements
df[] <- Map(function(x, y) replace(x, is.na(x), y), df, v1)
data
set.seed(24)
df <- as.data.frame(matrix(sample(c(NA, 1:5), 100*20, replace = TRUE), ncol = 20))
set.seed(48)
v1 <- sample(1:10, 20, replace = TRUE)
Replace NA with preceding character string in vector
tidyr
has a function fill
that fills in NA
s with the closest non-missing value above it.
If you're fine filling in values in X
in place:
library(dplyr)
library(tidyr)
d %>%
fill(X)
#> X Y
#> 1 one 1
#> 2 one 2
#> 3 two 3
#> 4 two 4
#> 5 three 5
#> 6 three 6
Or if you need to keep the original X
with its missing values, copy it over to another column, and fill that one in:
d %>%
mutate(X2 = X) %>%
fill(X2)
#> X Y X2
#> 1 one 1 one
#> 2 <NA> 2 one
#> 3 two 3 two
#> 4 <NA> 4 two
#> 5 three 5 three
#> 6 <NA> 6 three
Is there a way to replace a character in a vector with a NULL value in R?
It's worth remembering the difference between NULL
and NA
. NA
values are a dodgy value, NULL
is no value whatsoever. In order to get the second output to be the same as the first output, you would have something the same as the following
column <- c("None", "Some", "NULL", "Many", "All")
column <- column[column != "NULL"]
This creates a shorter vector, which is why str_replace doesn't like it.
Related Topics
Observeevent Shiny Function Used in a Module Does Not Work
What Does Passing an Ellipsis (...) as an Argument Mean in R
R: Save Multiple Plots from a File List into a Single File (Png or PDF or Other Format)
Tm: Read in Data Frame, Keep Text Id'S, Construct Dtm and Join to Other Dataset
Fast Way of Getting Index of Match in List
How to Count Occurrences Combinations in Data.Table in R
Align Plots Next to Each Other with Knitr
How Does One Turn Contour Lines into Filled Contours
How to Remove Specific Special Characters in R
Add Image (Png File) to Header of PDF File Created with R
Generating a Heatmap That Depicts the Clusters in a Dataset Using Hierarchical Clustering in R
Importing Wikipedia Tables in R
Drawing Simple Mediation Diagram in R
How to Pass Pandoc_Args to Yaml Header in Rmarkdown
Index Element from List in Rcpp
Split a Vector into Three Vectors of Unequal Length in R
Run Asynchronous Function in R
Merging Data Frames with Different Number of Rows and Different Columns