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)
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.
if/then replace values looping over rows conditional on column value(s) in R
We can get the data in long format and then use separate case_when
with conditions to recode values and finally get the data back in original format.
library(dplyr)
library(tidyr)
df %>%
pivot_longer(cols = -c(X1, X2)) %>%
mutate(value = case_when(X1 == 12 & X2 == 'DF1' & value == 'AB' ~ 1,
X1 == 12 & X2 == 'DF1' & value == 'AA' ~ 2,
X1 == 12 & X2 == 'DF1' & value == 'BB' ~ 3,
#Add more conditions as per requirements
#....
#If none of the above condition satisfy
#return a default value
TRUE ~ 0)) %>%
pivot_wider()
case_when
is an alternative to nested ifelse
statements since it makes to easy to write for various conditions in simple steps.
Replace values in R dataframe based on conditions
Yes! There's a command called replace
:
df$Age <- with(df, replace(Age, Age > 90 | Age < 16, NA))
Replace logical values conditionally in R
continuing from what @zephryl wrote, an even more readable code is:
screen$eligible <- with(screen,
(age > 17 & age < 23)
& (alcohol > 3 | marijuana > 3)
& (country == 0 | ageus < 12)
& county_1 %in% c(17, 27, 31)
& (residence_1 == 47))
- to detect where are the NAs:
sapply(screen, anyNA)
Conditional replacement of values in a data.frame
Since you are conditionally indexing df$est
, you also need to conditionally index the replacement vector df$a
:
index <- df$b == 0
df$est[index] <- (df$a[index] - 5)/2.533
Of course, the variable index
is just temporary, and I use it to make the code a bit more readible. You can write it in one step:
df$est[df$b == 0] <- (df$a[df$b == 0] - 5)/2.533
For even better readibility, you can use within
:
df <- within(df, est[b==0] <- (a[b==0]-5)/2.533)
The results, regardless of which method you choose:
df
a b est
1 11.77000 2 0.000000
2 10.90000 3 0.000000
3 10.32000 2 0.000000
4 10.96000 0 2.352941
5 9.90600 0 1.936834
6 10.70000 0 2.250296
7 11.43000 1 0.000000
8 11.41000 2 0.000000
9 10.48512 4 0.000000
10 11.19000 0 2.443743
As others have pointed out, an alternative solution in your example is to use ifelse
.
Replace all values in a data frame, conditionally
library(tidyverse)
df <- data.frame(
var1 = c(2L, 3L, 5L),
var2 = c(3L, 6L, 3L),
var3 = c(5L, 8L, 7L),
var4 = c(8L, 7L, 4L)
)
df %>%
mutate(across(.fns = ~ . >= 4)) %>%
summarise(across(.fns = ~ sum(.)/length(.) ))
#> var1 var2 var3 var4
#> 1 0.3333333 0.3333333 1 1
Related Topics
How to Fill a Geom_Area() Plot Using Ggplot
Export Each Data Frame Within a List to CSV
Run a Custom Function on a Data Frame in R, by Group
Linear Model and Dplyr - a Better Solution
How to Read a Password Protected Excel File into R
How to Insert (Add) a Row to a SQLite Db Table Using Dplyr Package
Interpolate Zoo Object with Missing Dates
How to Get Ggplot to Order Facets Correctly
Importing Only Every Nth Row from a .CSV File in R
Apply T-Test on Many Columns in a Dataframe Split by Factor
How to Conditionally Replace Values in R Data Frame Using If/Then Statement
How to Get Last Subelement of Every Element of a List
How to Order Bars in Faceted Ggplot2 Bar Chart
R Error in Unique.Default(X) Unique() Applies Only to Vectors
R: 'Split' Preserving Natural Order of Factors
Multiple Histograms with Ggplot2 - Position