R Ifelse to Replace Values in a Column

R ifelse to replace values in a column

This should work, using the working example:

var <- c("Private", "Private", "?", "Private")
df <- data.frame(var)
df$var[which(df$var == "?")] = "Private"

Then this will replace the values of "?" with "Private"

The reason your replacement isn't working (I think) is as if the value in df$var isn't "?" then it replaces the element of the vector with the whole df$var column, not just reinserting the element you want.

Mutate ifelse to replace values in r

It's better to use replace() from base R rather than ifelse() for these cases:

library(dplyr)

df %>%
mutate(Value1 = replace(Value1, Word == "nonWord", NA),
Value2 = replace(Value2, Word == "nonWord", NA))

#> Word Value1 Value2
#> 1 Apple True True
#> 2 Chair True False
#> 3 nonWord <NA> <NA>
#> 4 Hat False True

And if you're sure that all the columns you want to replace are named "Value...", you can take the advantage of mutate_at() from dplyr:

library(dplyr)

df %>%
mutate_at(vars(starts_with("Value")), ~ replace(., Word == "nonWord", NA))

#> Word Value1 Value2
#> 1 Apple True True
#> 2 Chair True False
#> 3 nonWord <NA> <NA>
#> 4 Hat False True

Data

df <- structure(list(Word = c("Apple", "Chair", "nonWord", "Hat"), 
Value1 = c("True", "True", "False", "False"), Value2 = c("True",
"False", "False", "True")), class = "data.frame",
row.names = c(NA, -4L))

How to conditionally replace values in r data frame using if/then statement

You can use ifelse, like this:

df$customer_id <- ifelse(df$customer %in% c('paramount', 'pixar'), 99, df$customer_id)

The syntax is simple:

ifelse(condition, result if TRUE, result if FALSE)

This is vectorized, so you can use it on a dataframe column.

ifelse replace value if it is lower than previous

I think cummax does exactly what you need.

Base R

dat$Reversal <- ave(dat$Reversal, dat$ID, FUN = cummax)
dat
# ID Owner Reversal Success
# 1 1 A 0 0
# 2 1 A 0 0
# 3 1 A 0 0
# 4 1 B 1 1
# 5 1 B 1 0
# 6 1 B 1 0
# 7 1 error 1 0
# 8 1 error 1 0
# 9 1 B 1 0
# 10 1 B 1 0
# 11 1 C 1 1
# 12 1 C 2 0
# 13 1 error 2 0
# 14 1 C 2 0
# 15 1 C 3 1
# 16 2 J 0 0
# 17 2 J 0 0

dplyr

dat %>%
group_by(ID) %>%
mutate(Reversal = cummax(Reversal)) %>%
ungroup()

data.table

as.data.table(dat)[, Reversal := cummax(Reversal), by = .(ID)][]

Data, courtesy of https://extracttable.com/

dat <- read.table(header = TRUE, text = "
ID Owner Reversal Success
1 A 0 0
1 A 0 0
1 A 0 0
1 B 1 1
1 B 1 0
1 B 1 0
1 error 0 0
1 error 0 0
1 B 1 0
1 B 1 0
1 C 1 1
1 C 2 0
1 error 0 0
1 C 2 0
1 C 3 1
2 J 0 0
2 J 0 0")

Replace all values in dataframe with an ifelse and %in% operator

Here are some base R options:

This maps a function over the columns in your data frame. Specifically the function tests if each element of the column is in your vector. This returns boolean values, which are then converted to 0 and 1 with as.numeric. The output of Map is a list, so data.frame converts it back to a data frame.

data.frame(Map(function(x) as.numeric(x %in% c(4,5)), df))

X1 X2 X3 X4 X5
1 0 0 0 0 1
2 0 0 1 0 0
3 0 1 0 1 0
4 1 1 0 1 0

Similarly,

data.frame(apply(df, 2, function(x) as.numeric(x %in% c(4,5))))

Alternatively, you can test the data frame against each vector element and then combine the results:

data.frame(Reduce(`+`, lapply(c(4,5), `==`, df)))

X1 X2 X3 X4 X5
1 0 0 0 0 1
2 0 0 1 0 0
3 0 1 0 1 0
4 1 1 0 1 0

lapply will test df == 4 and store that boolean matrix in a list element. Then it will test df == 5 and again store that in a list element. Reduce then sums these two list elements element-wise.

R: Using ifelse to change a value according to a value in a different column

Just leave it the same when the answer is NO:

db$State <- ifelse(db$hospital == "NYC Hospital", "New York", db$State)

Next time try using the code syntax

Applying ifelse in multiple variables / columns for replacing 99 and 999 to NA

Consider running ifelse on a block of columns since it works on vectors and matrices:

var_99 <- c("variable1", "variable2", "variable3", "variable4")
var_999 <- c("variable5", "variable6", "variable7")

dat[var_99] <- ifelse(dat[var_99] == 99, NA, dat$EC)
dat[var_999] <- ifelse(dat[var_999] == 999, NA, dat$EC)

For more than one variable replacement, coerce the no argument to matrix:

dat[var_99] <- ifelse(dat[var_99] == 99, NA, as.matrix(dat[var_99]))
dat[var_999] <- ifelse(dat[var_999] == 999, NA, as.matrix(dat[var_99]))

Replace a value in a data frame based on a conditional (`if`) statement

Easier to convert nm to characters and then make the change:

junk$nm <- as.character(junk$nm)
junk$nm[junk$nm == "B"] <- "b"

EDIT: And if indeed you need to maintain nm as factors, add this in the end:

junk$nm <- as.factor(junk$nm)

Change column values based on condition usign dplyr

Use the ifelse()

 df$stock<- ifelse(df$week == 0, 0, df$stock)

or in dplyr()

df %>%
mutate(stock=replace(stock, week==0, 0)) %>%
as.data.frame()

Sample data:

df<-data.frame("stock"=c(12334, 3456, 5678, 223232, 456, 678, 567, 1), 
"week"=c(0, 1,2,0,4,5,0,7))


Related Topics



Leave a reply



Submit