Change Column Value Based on Another Column, But Only for Certain Conditions in the First and Second Column (R)

Replacing values in a column based on a condition in another column

If you want to make sure that all thresholds are set to "High" when there is a hotspot, try:

long_fused <- mutate(long_fused, Threshold = if_else(Hotspot == "Yes", "High", Threshold))

Replace values of a column based on another column having as input a dataframe

We don't need any mapas 'a', 'b' and the number of rows of 'df' have the same length. So, an elementwise comparison with == can be done. Instead of replace, it may be better with ifelse/case_when etc as replace values should be of the same length as the list condition TRUE elements

library(dpyr)
df %>%
mutate(z = case_when(a == y ~ as.integer(b), TRUE ~ z))

-output

# A tibble: 3 x 2
# y z
# <int> <int>
#1 10 100
#2 11 200
#3 12 22

Or using base R

df$z <- with(df, ifelse(a == y, b, z))

In the OP's code, there is a difference in length when we do

 replace(x = z, y == .x, values = .y)

where 'z' will be the full column length, .x , .y will be each row element

Update

Based on the updated data, we could a join and then use coalesce

df %>% 
left_join(val, by = c('y' = 'a')) %>%
transmute(y, z = coalesce(b, z))
# A tibble: 4 x 2
# y z
# <dbl> <dbl>
#1 10 100
#2 11 21
#3 12 200
#4 13 23

Update a Value in One Column Based on Criteria in Other Columns

df <- data.frame(Name=c('John Smith', 'John Smith', 'Jeff Smith'),
State=c('MI','WI','WI'), stringsAsFactors=F)

df <- within(df, Name[Name == 'John Smith' & State == 'WI'] <- 'John Smith1')

> df
Name State
1 John Smith MI
2 John Smith1 WI
3 Jeff Smith WI

** Edit **

Edited to add that you can put whatever you like in the within expression:

df <- within(df, {
f <- Name == 'John Smith' & State == 'WI'
Name[f] <- 'John Smith1'
State[f] <- 'CA'
})

Replace values in one column condition on another column: R

You can use grepl to identify both "Skilled" and "Unskilled". Change those values from Employment column to Skill column and make Employment column as NA.

inds <- grepl('Skill', df$Employment)
df$Skill[inds] <- df$Employment[inds]
df$Employment[inds] <- NA
df

# Skill Employment
#1 Skilled A
#2 Unskilled B
#3 Skilled <NA>
#4 Skilled <NA>

Replace only certain values in column based on multiple conditions

I solved it using some functions of tidyverse, and I also added some other records to your example.

rm(list = ls(all=TRUE))

require(tidyverse)

df <- data.frame( ID=c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,3,3,3,3),
Concentration=c("XXX",0.3,0.7,0.6,"XXX","XXX",0.8,0.3,"XXX","XXX",
"XXX",0.6,0.1,0.1,"XXX",0.2,"XXX","XXX",1),
Time=c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,6,7,8,9))

df <- tibble(df) %>%
mutate(Concentration = as.character(Concentration),
Concentration_Original = Concentration) %>%
mutate(Concentration = ifelse(Concentration == 'XXX' & Time <= 3, "0", Concentration)) %>%
group_by(ID) %>%
mutate(Concentration = ifelse(Concentration == 'XXX' & Concentration == lead(Concentration),
"0.05", ifelse(Concentration == 'XXX',
"Missing", Concentration))) %>%
replace_na(list(Concentration = "Missing")) %>% ungroup()

Change column value based on another column, but only for certain conditions in the first AND second column (R)

I also worked out the following way of doing it, which seems a bit neater and more intuitive to me. No need to convert to numeric.

city_data$city[grepl("Unknown", city_data$city) & 
grepl("London|Camden|Westminster", city_data$city_details)] <- "London"

Replace Dataframe column with another dataframe based on conditions - R

I think you can use the following solution:

library(dplyr)

df1 %>%
left_join(df2, by = c("ID1", "ID2")) %>%
mutate(VALUE1.x = ifelse(ID1 == 5 & ID2 < 100, VALUE1.y, VALUE1.x)) %>%
select(-VALUE1.y) %>%
rename_with(~ sub("\\.x", "", .), contains(".x"))

ID1 ID2 VALUE1 NAME SURNAME
1 1 10 100 Juan perez
2 2 20 200 Rodrigo jones
3 3 30 300 Pedro bla
4 4 40 400 Lucas lopez
5 5 50 40 d martinez
6 5 150 100 e rodriguez
7 5 200 200 f jerez
8 4 99 40 g dieguez
9 3 10 150 x gimenez
10 5 25 200 a mendez

Replacing values from a column using a condition in R

# reassign depth values under 10 to zero
df$depth[df$depth<10] <- 0

(For the columns that are factors, you can only assign values that are factor levels. If you wanted to assign a value that wasn't currently a factor level, you would need to create the additional level first:

levels(df$species) <- c(levels(df$species), "unknown") 
df$species[df$depth<10] <- "unknown"

extract a column in dataframe based on condition for another column R

You can use the subset function in base R -

subset(df, g == 'b', select = b)

# b
#bb 2
#cc 3


Related Topics



Leave a reply



Submit