Can dplyr package be used for conditional mutating?
Use ifelse
df %>%
mutate(g = ifelse(a == 2 | a == 5 | a == 7 | (a == 1 & b == 4), 2,
ifelse(a == 0 | a == 1 | a == 4 | a == 3 | c == 4, 3, NA)))
Added - if_else: Note that in dplyr 0.5 there is an if_else
function defined so an alternative would be to replace ifelse
with if_else
; however, note that since if_else
is stricter than ifelse
(both legs of the condition must have the same type) so the NA
in that case would have to be replaced with NA_real_
.
df %>%
mutate(g = if_else(a == 2 | a == 5 | a == 7 | (a == 1 & b == 4), 2,
if_else(a == 0 | a == 1 | a == 4 | a == 3 | c == 4, 3, NA_real_)))
Added - case_when Since this question was posted dplyr has added case_when
so another alternative would be:
df %>% mutate(g = case_when(a == 2 | a == 5 | a == 7 | (a == 1 & b == 4) ~ 2,
a == 0 | a == 1 | a == 4 | a == 3 | c == 4 ~ 3,
TRUE ~ NA_real_))
Added - arithmetic/na_if If the values are numeric and the conditions (except for the default value of NA at the end) are mutually exclusive, as is the case in the question, then we can use an arithmetic expression such that each term is multiplied by the desired result using na_if
at the end to replace 0 with NA.
df %>%
mutate(g = 2 * (a == 2 | a == 5 | a == 7 | (a == 1 & b == 4)) +
3 * (a == 0 | a == 1 | a == 4 | a == 3 | c == 4),
g = na_if(g, 0))
dplyr mutate with conditional values
Try this:
myfile %>% mutate(V5 = (V1 == 1 & V2 != 4) + 2 * (V2 == 4 & V3 != 1))
giving:
V1 V2 V3 V4 V5
1 1 2 3 5 1
2 2 4 4 1 2
3 1 4 1 1 0
4 4 5 1 3 0
5 5 5 5 4 0
or this:
myfile %>% mutate(V5 = ifelse(V1 == 1 & V2 != 4, 1, ifelse(V2 == 4 & V3 != 1, 2, 0)))
giving:
V1 V2 V3 V4 V5
1 1 2 3 5 1
2 2 4 4 1 2
3 1 4 1 1 0
4 4 5 1 3 0
5 5 5 5 4 0
Note
Suggest you get a better name for your data frame. myfile makes it seem as if it holds a file name.
Above used this input:
myfile <-
structure(list(V1 = c(1L, 2L, 1L, 4L, 5L), V2 = c(2L, 4L, 4L,
5L, 5L), V3 = c(3L, 4L, 1L, 1L, 5L), V4 = c(5L, 1L, 1L, 3L, 4L
)), .Names = c("V1", "V2", "V3", "V4"), class = "data.frame", row.names = c("1",
"2", "3", "4", "5"))
Update 1 Since originally posted dplyr has changed %.%
to %>%
so have modified answer accordingly.
Update 2 dplyr now has case_when
which provides another solution:
myfile %>%
mutate(V5 = case_when(V1 == 1 & V2 != 4 ~ 1,
V2 == 4 & V3 != 1 ~ 2,
TRUE ~ 0))
Combine mutate with conditional statement using a formula in r
Here is one potential solution:
library(tidyverse)
df <- read.table(text = " t1 t2 Rg Rg_SE
1 WT Fat 0.6818 0.0962
2 WT FatPC 0.1853 0.1354
3 WT DTD -0.1684 0.1232
4 Fat DefCode NA 0.1608", header = TRUE)
df %>%
mutate(Rg = ifelse((Rg_SE*2) > abs(as.numeric(as.character(Rg))), NA, Rg))
# t1 t2 Rg Rg_SE
#1 WT Fat 0.6818 0.0962
#2 WT FatPC NA 0.1354
#3 WT DTD NA 0.1232
#4 Fat DefCode NA 0.1608
dplyr conditional mutate with ifelse
An option is to insert the *
with a regex
library(dplyr)
library(stringr)
df %>%
mutate(across(Kingdom:Genus, ~str_replace(.x, "(Incertae sedis)", "*\\1*")))
Related Topics
Use Trycatch Skip to Next Value of Loop Upon Error
Select Every Other Element from a Vector
Rstudio Rmarkdown: Both Portrait and Landscape Layout in a Single PDF
How to Install an R Package from the Source Tarball on Windows
R Compare Multiple Values with Vector and Return Vector
What Does the Capital Letter "I" in R Linear Regression Formula Mean
Count Number of Columns by a Condition (>) for Each Row
Split Delimited Single Value Character Vector
Add a "Rank" Column to a Data Frame
Using R to List All Files with a Specified Extension
Extract Matrix Column Values by Matrix Column Name
Convert Column Classes in Data.Table
Count Number of Zeros Per Row, and Remove Rows with More Than N Zeros
What Are the R Sorting Rules of Character Vectors
Creating a Plot Window of a Particular Size