Replace a value NA with the value from another column in R
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)
how to replace NA with the values from another column
You can use mutate
to apply an ifelse
across every column.
library(dplyr)
df.NA %>%
mutate(across(everything(), ~ ifelse(is.na(.x), a, .x)))
#> a b c d
#> 1 value1 value1 9 value1
#> 2 value2 6 value2 value2
#> 3 value3 7 value3 value3
#> 4 value4 value4 10 12
#> 5 value5 8 11 13
R: Change NA in 1000s of columns to the value of another column
Turn the columns to characters and replace the NA
values with the corresponding var_fill
value.
dta1$var_fill <- as.character(dta1$var_fill)
dta1[vars] <- lapply(dta1[vars], function(x) {
x <- as.character(x)
x[is.na(x)] <- dta1$var_fill[is.na(x)]
x
})
In dplyr
, you can use coalesce
.
library(dplyr)
dta1 <- dta1 %>% mutate(across(all_of(vars), ~coalesce(., var_fill)))
Replace NA values with value from another column in same dataframe
I would try with standard subsetting:
#subset the NAs of new price with the ones from price
df$NewPrice[is.na(df$NewPrice)] <- df$Price[is.na(df$NewPrice)]
Out:
df
# Item From Price Discount NewPrice
#1 A Delhi 100 0.1 110
#2 A Mumbai 200 0.1 120
#3 A Pune 150 NA 150
#4 A Nagpur 200 0.1 200
Manually replace missing value in a column based on another column
Does this work. Not sure if in your data you'd have NA
for all values of geo == ny
. Hence I've added & is.na(mark)
.
library(dplyr)
df %>% mutate(mark = case_when(geo == 'ny' & is.na(mark) ~ 'toyota', TRUE ~ mark))
# A tibble: 5 x 3
geo mark value
<chr> <chr> <dbl>
1 texas nissan 2
2 texas nissan 78
3 ny toyota 65
4 ny toyota 15
5 ca audi 22
Replacing NAs in a column with the values of other column
You can use coalesce
:
library(dplyr)
df1 <- data.frame(Letters, Char, stringsAsFactors = F)
df1 %>%
mutate(Char1 = coalesce(Char, Letters))
Letters Char Char1
1 A a a
2 B b b
3 C <NA> C
4 D d d
5 E <NA> E
How to replace NA in a dataframe for a specific value using the results of another column and taking into account conditions of another column?
Some of them are not exact matches, so use str_detect
library(dplyr)
library(stringr)
df %>%
mutate(Weight = case_when(is.na(Weight) &
str_detect(Product, regex("Bread", ignore_case = TRUE)) ~ 2.5 * Units,
is.na(Weight) & Product == "Eggs"~ Units, TRUE ~ Weight))
-output
# A tibble: 5 × 4
Product Weight Units Price
<chr> <dbl> <dbl> <dbl>
1 Bread 5 2 1
2 Oranges 1 6 3.5
3 Eggs 1 1 0.5
4 Bananas NA 2 0.75
5 Whole Bread 2.5 1 1.5
R Replacing NA values with the next value of another column value within groups
Here's is a possible dplyr
solution. This is a combination of ifelse
and lead
, while the end product should be converted to as.POSIXct
again as a result of lost information due to the use of ifelse
library(dplyr)
tmpdf %>%
group_by(spaceNum) %>%
mutate(time.OUT = as.POSIXct(ifelse(is.na(time.OUT), lead(time.IN), time.OUT), origin = "1970-01-01"))
# Source: local data frame [7 x 3]
# Groups: spaceNum
#
# spaceNum time.IN time.OUT
# 1 1 2015-09-04 16:30:00 2015-09-04 18:00:00
# 2 1 2015-09-04 19:50:00 2015-09-04 21:00:00
# 3 1 2015-09-04 21:00:00 <NA>
# 4 2 2015-09-05 12:00:00 2015-09-05 13:00:00
# 5 2 2015-09-05 13:00:00 2015-09-05 13:21:00
# 6 2 2015-09-05 16:00:00 2015-09-05 16:48:00
# 7 2 2015-09-05 17:00:00 <NA>
Related Topics
Rscript: There Is No Package Called ...
How to Make Time Difference in Same Units When Subtracting Posixct
Different Breaks Per Facet in Ggplot2 Histogram
Shift Values in Single Column of Dataframe Up
Rstudio Shiny Error: There Is No Package Called "Shinydashboard"
How to Make Grouped Layout in Igraph
Shiny: Passing Input$Var to Aes() in Ggplot2
Join R Data.Tables Where Key Values Are Not Exactly Equal--Combine Rows with Closest Times
Select Row with Most Recent Date by Group
Add a Box for the Na Values to the Ggplot Legend for a Continuous Map
Modify X-Axis Labels in Each Facet
Perform Multiple Paired T-Tests Based on Groups/Categories
R Semicolon Delimited a Column into Rows
Conditional Binary Join and Update by Reference Using the Data.Table Package
In Ggplot2, What Do the End of the Boxplot Lines Represent