Dplyr Replacing Na Values in a Column Based on Multiple Conditions

dplyr: Replace multiple values based on condition in a selection of columns

A dplyr solution:

library(dplyr)
dt %>%
mutate(across(3:5, ~ ifelse(measure == "led", stringr::str_replace_all(
as.character(.),
c("2" = "X", "3" = "Y")
), .)))

Result:

   measure site space qty qty.exit cf
1: led 4 1 4 6 3
2: exit 4 2 1 4 6
3: cfl 1 4 6 2 3
4: linear 3 4 1 3 5
5: cfl 5 1 6 1 6
6: exit 4 3 2 6 4
7: exit 5 1 4 2 5
8: exit 1 4 3 6 4
9: linear 3 1 5 4 1
10: led 4 1 1 1 1
11: exit 5 4 3 5 2
12: cfl 4 2 4 5 5
13: led 4 X Y Y 4
...

How to replace the value with multiple conditions in R

Update: thank to valuable comment of Onyambu:

jimma3n<-jimma3 %>%
select(Enterdateofexam,Enterdateofexam2, Enterdayofexam, UniqueKey,MEDICALRECORD)%>%
mutate(
day= case_when(UniqueKey == 1234 | UniqueKey= 1321 & day==80 ~ 8,
UniqueKey == 1234 | UniqueKey= 1321 & day==70 ~ 7,
UniqueKey == 1484 & day==79 ~ 7,
TRUE ~ day),
month = case_when(UniqueKey== 1484 & month==79 ~ 09,
TRUE ~ month)
)

OK try this and tell me please:

jimma3n<-jimma3 %>%
select(Enterdateofexam,Enterdateofexam2, Enterdayofexam, UniqueKey,MEDICALRECORD)%>%
mutate(
day= case_when(UniqueKey == 1234 | UniqueKey= 1321 & day==80 ~ 8,
UniqueKey == 1234 | UniqueKey= 1321 & day==70 ~ 7,
UniqueKey == 1484 & day==79 ~ 7,
TRUE ~ day)
) %>%
mutate(
month = case_when(UniqueKey== 1484 & month==79 ~ 09,
TRUE ~ month)
)

Replace NA values with preexisting value based on multiple conditions

Try using ifelse() to apply the same condition.

df$Box_Height <- ifelse(is.na(df$Box_Height) & !is.na(df$Item_Height), df$Item_Height, df$Box_Height)

The ifelse() function requires you to provide values for cases where the condition is true and false separately which ensures that the vector lengths will match. Subsetting df$Box_Height with [ probably results in a vector that is shorter than df$Item_Height, which is unsubsetted.

R replace_na values conditionally by column with multiple conditions

First compute a logical vector, all.na having one component per row which is TRUE if that row's numeric data is all NAs and FALSE otherwise. Then use na.aggregate to fill in all-NA rows. Also use na.aggregate on 2007. Then convert to long form and apply na.locf0 by country and convert back to wide form.

library(dplyr)
library(tidyr)
library(zoo)

all.na <- sample %>%
select_if(is.numeric) %>%
{ rowSums(is.na(.)) == ncol(.) }

sample %>%
mutate_at(-(1:3), ~ if_else(all.na, na.aggregate(.x), .x)) %>%
mutate(`2007` = na.aggregate(`2007`)) %>%
gather(key, value, -`Country Name`, -`Country Code`) %>%
group_by(`Country Name`, `Country Code`) %>%
mutate(value = na.locf0(value)) %>%
ungroup %>%
spread(key, value)

or using only zoo:

library(zoo)

all.na <- apply(is.na(sample[grep("^2", names(sample))]), 1, all)

ix <- -(1:3)
sample.out <- sample
Fill <- function(x) ifelse(all.na, na.aggregate(x), x)
sample.out[ix] <- lapply(sample[ix], Fill)
sample.out$"2007" <- na.aggregate(sample.out$"2007")
sample.out[ix] <- t(apply(sample.out[ix], 1, na.locf0))

Replace NAs in dataframe with values from second dataframe based on multiple criteria

You can create a unique key to update df2.

unique_key1 <- paste(df1$A, df1$B)
unique_key2 <- paste(df2$A, df2$B)
inds <- is.na(df2$C)
df2$C[inds] <- df1$C[match(unique_key2[inds], unique_key1)]
df2

# A B C E
#1 20210901 15:00 74 A 74
#2 20210903 17:00 27 C 27
#3 20210904 18:00 60 D 60
#4 20210906 20:00 7 F 7
#5 20210907 21:00 96 G 96
#6 20210908 22:00 98 H 98
#7 20210909 23:00 38 I 38
#8 20210910 00:00 89 J 89
#9 20210912 02:00 69 L 69
#10 20210913 03:00 72 M 72
#11 20210914 04:00 76 N 76
#12 20210915 05:00 63 O 63
#13 20210916 06:00 13 P 13
#14 20210918 08:00 25 R 25
#15 20210919 09:00 92 S 92
#16 20210920 10:00 21 T 21
#17 20210921 11:00 79 U 79
#18 20210922 12:00 41 V 41
#19 20210924 14:00 97 X 97
#20 20210925 15:00 16 Y 16

data

cbind creates a matrix, use data.frame to create dataframes.

df1 <- data.frame(A, B, C, D)
df2 <- data.frame(A, B, C, E)

R replace multiple columns based on multiple conditions

Try this:

test %>% 
mutate(substart = ifelse(value <= -1 & is.na(substart),start,substart),
subend = ifelse(value <= -1 & is.na(subend),end,subend))

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


Related Topics



Leave a reply



Submit