Changing Multiple Column Values Given a Condition in Dplyr

changing multiple column values given a condition in dplyr

You can use mutate_at and pass the columns x1,x2,x3 to .vars parameter:

dta <- data.frame(na.ind = 1:3, x1 = 2:4, x2 = 2:4, x3 = 2:4, x4 = 2:4)
dta
# na.ind x1 x2 x3 x4
#1 1 2 2 2 2
#2 2 3 3 3 3
#3 3 4 4 4 4

dta %>% mutate_at(.vars = c("x1", "x2", "x3"), funs(ifelse(na.ind == 1, NA, .)))
# na.ind x1 x2 x3 x4
#1 1 NA NA NA 2
#2 2 3 3 3 3
#3 3 4 4 4 4

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
...

R, dplyr, Conditionally change values in multiple columns based on one conditional column in R

Do you mean this?

cols <- c("a", "b", "c")
df[is.na(df$d) | df$d == 0, cols] <- NA
df
# a b c d
#1 NA NA NA NA
#2 2 3 4 1
#3 3 4 5 1
#4 NA NA NA 0
#5 NA NA NA 0

Or in dplyr

library(dplyr)
df %>% mutate_at(vars(a:c), funs(ifelse(is.na(d) | d == 0, NA, .)))
# a b c d
#1 NA NA NA NA
#2 2 3 4 1
#3 3 4 5 1
#4 NA NA NA 0
#5 NA NA NA 0

Replace values in multiple columns by values of another column on condition

One solution is to use mutate and case_when from dplyr

library(dplyr)

df <- df %>%
mutate(var2 = case_when(var1 %in% c('02','03') ~ '0',
TRUE ~ as.character(var2)),
var3 = case_when(var1 %in% c('02','03') ~ '00',
TRUE ~ as.character(var3)),
var4 = case_when(var1 %in% c('02','03') ~ '000',
TRUE ~ as.character(var4)))

Condition on multiple columns and change the values using R

With dplyr we can use tidyselect syntax inside across() to select all variables starting with "title_" and then apply a function on all selected columns inside across():

data<-data.frame(id=c(1,1,1,2,2,2,2),
title_1=c(65,58,47,NA,25,27,43),
title_2=c(NA,NA,32,35,12,NA,1))

library(dplyr)

data %>%
mutate(across(starts_with("title_"), ~ ifelse(is.na(.x), 0, 1)))
#> id title_1 title_2
#> 1 1 1 0
#> 2 1 1 0
#> 3 1 1 1
#> 4 2 0 1
#> 5 2 1 1
#> 6 2 1 0
#> 7 2 1 1

In base R we would use grepl to select the column names, then assign those columns new values with lapply:

data<-data.frame(id=c(1,1,1,2,2,2,2),
title_1=c(65,58,47,NA,25,27,43),
title_2=c(NA,NA,32,35,12,NA,1))

mycols <- grepl("^title_", names(data))

data[mycols] <- lapply(data[mycols], \(x) ifelse(is.na(x), 0, 1))
data

#> id title_1 title_2
#> 1 1 1 0
#> 2 1 1 0
#> 3 1 1 1
#> 4 2 0 1
#> 5 2 1 1
#> 6 2 1 0
#> 7 2 1 1

Finally, we would select the columns with data.table similary, but here we'd prefer the actual names with grep(value = TRUE):

mycols <- grep("^title_", names(data), value = TRUE)

library(data.table)

data_tb <- as.data.table(data)

data_tb[,
get("mycols") := lapply(.SD, \(x) ifelse(is.na(x), 0, 1)),
.SDcols = mycols]

data_tb
#> id title_1 title_2
#> 1: 1 1 0
#> 2: 1 1 0
#> 3: 1 1 1
#> 4: 2 0 1
#> 5: 2 1 1
#> 6: 2 1 0
#> 7: 2 1 1

Created on 2022-07-26 by the reprex package (v2.0.1)

Changing multiple column values at once for a single row in my dataframe

You can use the new rows_update function written for such tasks.

library(dplyr)

row <- tibble(ID = 'C', State = 'StateB', Town = 'Town3', Street = 'StreetZ')
result <- rows_update(df, row, by = 'ID')

# ID State Town Street
#1 A StateA Town1 StreetX
#2 B StateA Town2 StreetY
#3 C StateB Town3 StreetZ
#4 D StateB Town4 StreetQ
#5 E StateC Town5 StreetK
#6 F StateC Town6 StreetN

How to replace values by multiple grouping conditions?

How about this:

library(tidyverse)
#> Warning: package 'tidyr' was built under R version 4.1.2
#> Warning: package 'readr' was built under R version 4.1.2

df <- tibble(id = c(rep("A1",11), rep("B1",11)),
Model = rep(c(rep("ACT",5), rep("ML1",3), rep("ML2",3)),2),
Date = as.Date(rep(c("2010-01-01","2011-01-01","2012-01-01","2013-01-01",
"2014-01-01",NA, "2015-01-01","2016-01-01",
NA, "2015-01-01","2016-01-01"),2)),
Value = c(c(11,31,44,21,54,NA,53,13,NA,33,12),
c(54,41,32,65,76,NA,32,42,NA,23,76))
)
df %>%
group_by(id) %>%
filter(Model == "ACT") %>%
summarise(across(c(Date, Value), last)) %>%
rename(date_fill = Date, value_fill = Value) %>%
right_join(df) %>%
mutate(Value = case_when(Model != "Act" & is.na(Value) ~ value_fill, TRUE ~ Value),
Date = case_when(Model != "Act" & is.na(Date) ~ date_fill, TRUE ~ Date)) %>%
select(-c("date_fill", "value_fill"))
#> Joining, by = "id"
#> # A tibble: 22 × 4
#> id Model Date Value
#> <chr> <chr> <date> <dbl>
#> 1 A1 ACT 2010-01-01 11
#> 2 A1 ACT 2011-01-01 31
#> 3 A1 ACT 2012-01-01 44
#> 4 A1 ACT 2013-01-01 21
#> 5 A1 ACT 2014-01-01 54
#> 6 A1 ML1 2014-01-01 54
#> 7 A1 ML1 2015-01-01 53
#> 8 A1 ML1 2016-01-01 13
#> 9 A1 ML2 2014-01-01 54
#> 10 A1 ML2 2015-01-01 33
#> # … with 12 more rows

Created on 2022-03-28 by the reprex package (v2.0.1)



Related Topics



Leave a reply



Submit