How to write ifelse statement with multiple conditions in R?
up vote
1
down vote
favorite
I have a problem in writing ifelse statement ,I have three columns as shown below:
Team 1 Winner
T1 T1
T2 T1
T2 NA
T3 NA
I want another column : Result such that if Team=Winner it should be Winner else losser and If Team=anything & winner=NA then it should be no result...
Team 1 Winner result
T1 T1 winner
T2 T1 losser
T2 NA noresult
T3 NA noresult
Any help would be appreciated.
r
add a comment |
up vote
1
down vote
favorite
I have a problem in writing ifelse statement ,I have three columns as shown below:
Team 1 Winner
T1 T1
T2 T1
T2 NA
T3 NA
I want another column : Result such that if Team=Winner it should be Winner else losser and If Team=anything & winner=NA then it should be no result...
Team 1 Winner result
T1 T1 winner
T2 T1 losser
T2 NA noresult
T3 NA noresult
Any help would be appreciated.
r
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a problem in writing ifelse statement ,I have three columns as shown below:
Team 1 Winner
T1 T1
T2 T1
T2 NA
T3 NA
I want another column : Result such that if Team=Winner it should be Winner else losser and If Team=anything & winner=NA then it should be no result...
Team 1 Winner result
T1 T1 winner
T2 T1 losser
T2 NA noresult
T3 NA noresult
Any help would be appreciated.
r
I have a problem in writing ifelse statement ,I have three columns as shown below:
Team 1 Winner
T1 T1
T2 T1
T2 NA
T3 NA
I want another column : Result such that if Team=Winner it should be Winner else losser and If Team=anything & winner=NA then it should be no result...
Team 1 Winner result
T1 T1 winner
T2 T1 losser
T2 NA noresult
T3 NA noresult
Any help would be appreciated.
r
r
edited 15 hours ago
RLave
2,8341820
2,8341820
asked 15 hours ago
Praveen Chougale
737
737
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
1
down vote
accepted
Use -
df$Winner <- factor(df[,2], levels=unique(df$Team.1)) # avoid "level sets of factors are different" error
df$result <- ifelse(df$Team.1 == df$Winner, "winner", "loser")
df[is.na(df$result), "result"] <- "noresult"
df
Output
Team.1 Winner result
1 T1 T1 winner
2 T2 T1 loser
3 T2 <NA> noresult
4 T3 <NA> noresult
add a comment |
up vote
2
down vote
Another possibility is with case_when from dplyr:
library(dplyr)
df %>%
mutate(Result = case_when(
Team == Winner ~ "Winner",
Team != Winner ~ "Loser",
is.na(Winner) ~ "No result"
))
# Team Winner Result
# 1 T1 T1 Winner
# 2 T2 T1 Loser
# 3 T2 <NA> No result
# 4 T3 <NA> No result
Data:
tt <- "Team Winner
T1 T1
T2 T1
T2 NA
T3 NA"
df <- read.table(text=tt, header = T, stringsAsFactors = F)
add a comment |
up vote
2
down vote
You can use dplyr::if_else(), as I learned, it is strict, because it checks the data type and it handles the NAs, making code simpler:
df %>% mutate(Result = if_else( Team==Winner, "Winner", "Loser", missing ='No result'))
Team Winner Result
1 T1 T1 Winner
2 T2 T1 Loser
3 T2 <NA> No result
4 T3 <NA> No result
Despite, looking at the one-liner solution here, for your example data, it's not the fastest (the winner is the @Tim Biegeleisen 's answer, +1):
Unit: microseconds
expr min lq mean median uq max neval cld
IF_ELSE 893.013 974.5060 1176.35331 1053.2260 1343.3590 2278.398 100 b
IFELSE 20.481 34.3475 49.57934 47.3605 58.0275 143.361 100 a
CASE 1067.946 1152.4255 1423.41426 1226.0255 1721.3850 4108.795 100 c
So I can figure out a trade off between simplicity (that is subjective, of course) and more control (that is objective, due the nature of the functions), and velocity (if it's an issue to you, looking your real data, but it's more objective).
add a comment |
up vote
1
down vote
Try this logic:
df$result <- ifelse(is.na(df$Winner), "no result",
ifelse(df$Team==df$Winner, "winner", "loser"))
df
Team Winner result
1 T1 T1 winner
2 T2 T1 loser
3 T2 <NA> no result
4 T3 <NA> no result
This is give NA where I want it to be losser
– Praveen Chougale
15 hours ago
Answer updated.
– Tim Biegeleisen
15 hours ago
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Use -
df$Winner <- factor(df[,2], levels=unique(df$Team.1)) # avoid "level sets of factors are different" error
df$result <- ifelse(df$Team.1 == df$Winner, "winner", "loser")
df[is.na(df$result), "result"] <- "noresult"
df
Output
Team.1 Winner result
1 T1 T1 winner
2 T2 T1 loser
3 T2 <NA> noresult
4 T3 <NA> noresult
add a comment |
up vote
1
down vote
accepted
Use -
df$Winner <- factor(df[,2], levels=unique(df$Team.1)) # avoid "level sets of factors are different" error
df$result <- ifelse(df$Team.1 == df$Winner, "winner", "loser")
df[is.na(df$result), "result"] <- "noresult"
df
Output
Team.1 Winner result
1 T1 T1 winner
2 T2 T1 loser
3 T2 <NA> noresult
4 T3 <NA> noresult
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Use -
df$Winner <- factor(df[,2], levels=unique(df$Team.1)) # avoid "level sets of factors are different" error
df$result <- ifelse(df$Team.1 == df$Winner, "winner", "loser")
df[is.na(df$result), "result"] <- "noresult"
df
Output
Team.1 Winner result
1 T1 T1 winner
2 T2 T1 loser
3 T2 <NA> noresult
4 T3 <NA> noresult
Use -
df$Winner <- factor(df[,2], levels=unique(df$Team.1)) # avoid "level sets of factors are different" error
df$result <- ifelse(df$Team.1 == df$Winner, "winner", "loser")
df[is.na(df$result), "result"] <- "noresult"
df
Output
Team.1 Winner result
1 T1 T1 winner
2 T2 T1 loser
3 T2 <NA> noresult
4 T3 <NA> noresult
answered 15 hours ago
Vivek Kalyanarangan
3,9521725
3,9521725
add a comment |
add a comment |
up vote
2
down vote
Another possibility is with case_when from dplyr:
library(dplyr)
df %>%
mutate(Result = case_when(
Team == Winner ~ "Winner",
Team != Winner ~ "Loser",
is.na(Winner) ~ "No result"
))
# Team Winner Result
# 1 T1 T1 Winner
# 2 T2 T1 Loser
# 3 T2 <NA> No result
# 4 T3 <NA> No result
Data:
tt <- "Team Winner
T1 T1
T2 T1
T2 NA
T3 NA"
df <- read.table(text=tt, header = T, stringsAsFactors = F)
add a comment |
up vote
2
down vote
Another possibility is with case_when from dplyr:
library(dplyr)
df %>%
mutate(Result = case_when(
Team == Winner ~ "Winner",
Team != Winner ~ "Loser",
is.na(Winner) ~ "No result"
))
# Team Winner Result
# 1 T1 T1 Winner
# 2 T2 T1 Loser
# 3 T2 <NA> No result
# 4 T3 <NA> No result
Data:
tt <- "Team Winner
T1 T1
T2 T1
T2 NA
T3 NA"
df <- read.table(text=tt, header = T, stringsAsFactors = F)
add a comment |
up vote
2
down vote
up vote
2
down vote
Another possibility is with case_when from dplyr:
library(dplyr)
df %>%
mutate(Result = case_when(
Team == Winner ~ "Winner",
Team != Winner ~ "Loser",
is.na(Winner) ~ "No result"
))
# Team Winner Result
# 1 T1 T1 Winner
# 2 T2 T1 Loser
# 3 T2 <NA> No result
# 4 T3 <NA> No result
Data:
tt <- "Team Winner
T1 T1
T2 T1
T2 NA
T3 NA"
df <- read.table(text=tt, header = T, stringsAsFactors = F)
Another possibility is with case_when from dplyr:
library(dplyr)
df %>%
mutate(Result = case_when(
Team == Winner ~ "Winner",
Team != Winner ~ "Loser",
is.na(Winner) ~ "No result"
))
# Team Winner Result
# 1 T1 T1 Winner
# 2 T2 T1 Loser
# 3 T2 <NA> No result
# 4 T3 <NA> No result
Data:
tt <- "Team Winner
T1 T1
T2 T1
T2 NA
T3 NA"
df <- read.table(text=tt, header = T, stringsAsFactors = F)
answered 15 hours ago
RLave
2,8341820
2,8341820
add a comment |
add a comment |
up vote
2
down vote
You can use dplyr::if_else(), as I learned, it is strict, because it checks the data type and it handles the NAs, making code simpler:
df %>% mutate(Result = if_else( Team==Winner, "Winner", "Loser", missing ='No result'))
Team Winner Result
1 T1 T1 Winner
2 T2 T1 Loser
3 T2 <NA> No result
4 T3 <NA> No result
Despite, looking at the one-liner solution here, for your example data, it's not the fastest (the winner is the @Tim Biegeleisen 's answer, +1):
Unit: microseconds
expr min lq mean median uq max neval cld
IF_ELSE 893.013 974.5060 1176.35331 1053.2260 1343.3590 2278.398 100 b
IFELSE 20.481 34.3475 49.57934 47.3605 58.0275 143.361 100 a
CASE 1067.946 1152.4255 1423.41426 1226.0255 1721.3850 4108.795 100 c
So I can figure out a trade off between simplicity (that is subjective, of course) and more control (that is objective, due the nature of the functions), and velocity (if it's an issue to you, looking your real data, but it's more objective).
add a comment |
up vote
2
down vote
You can use dplyr::if_else(), as I learned, it is strict, because it checks the data type and it handles the NAs, making code simpler:
df %>% mutate(Result = if_else( Team==Winner, "Winner", "Loser", missing ='No result'))
Team Winner Result
1 T1 T1 Winner
2 T2 T1 Loser
3 T2 <NA> No result
4 T3 <NA> No result
Despite, looking at the one-liner solution here, for your example data, it's not the fastest (the winner is the @Tim Biegeleisen 's answer, +1):
Unit: microseconds
expr min lq mean median uq max neval cld
IF_ELSE 893.013 974.5060 1176.35331 1053.2260 1343.3590 2278.398 100 b
IFELSE 20.481 34.3475 49.57934 47.3605 58.0275 143.361 100 a
CASE 1067.946 1152.4255 1423.41426 1226.0255 1721.3850 4108.795 100 c
So I can figure out a trade off between simplicity (that is subjective, of course) and more control (that is objective, due the nature of the functions), and velocity (if it's an issue to you, looking your real data, but it's more objective).
add a comment |
up vote
2
down vote
up vote
2
down vote
You can use dplyr::if_else(), as I learned, it is strict, because it checks the data type and it handles the NAs, making code simpler:
df %>% mutate(Result = if_else( Team==Winner, "Winner", "Loser", missing ='No result'))
Team Winner Result
1 T1 T1 Winner
2 T2 T1 Loser
3 T2 <NA> No result
4 T3 <NA> No result
Despite, looking at the one-liner solution here, for your example data, it's not the fastest (the winner is the @Tim Biegeleisen 's answer, +1):
Unit: microseconds
expr min lq mean median uq max neval cld
IF_ELSE 893.013 974.5060 1176.35331 1053.2260 1343.3590 2278.398 100 b
IFELSE 20.481 34.3475 49.57934 47.3605 58.0275 143.361 100 a
CASE 1067.946 1152.4255 1423.41426 1226.0255 1721.3850 4108.795 100 c
So I can figure out a trade off between simplicity (that is subjective, of course) and more control (that is objective, due the nature of the functions), and velocity (if it's an issue to you, looking your real data, but it's more objective).
You can use dplyr::if_else(), as I learned, it is strict, because it checks the data type and it handles the NAs, making code simpler:
df %>% mutate(Result = if_else( Team==Winner, "Winner", "Loser", missing ='No result'))
Team Winner Result
1 T1 T1 Winner
2 T2 T1 Loser
3 T2 <NA> No result
4 T3 <NA> No result
Despite, looking at the one-liner solution here, for your example data, it's not the fastest (the winner is the @Tim Biegeleisen 's answer, +1):
Unit: microseconds
expr min lq mean median uq max neval cld
IF_ELSE 893.013 974.5060 1176.35331 1053.2260 1343.3590 2278.398 100 b
IFELSE 20.481 34.3475 49.57934 47.3605 58.0275 143.361 100 a
CASE 1067.946 1152.4255 1423.41426 1226.0255 1721.3850 4108.795 100 c
So I can figure out a trade off between simplicity (that is subjective, of course) and more control (that is objective, due the nature of the functions), and velocity (if it's an issue to you, looking your real data, but it's more objective).
edited 14 hours ago
answered 15 hours ago
s_t
2,5782925
2,5782925
add a comment |
add a comment |
up vote
1
down vote
Try this logic:
df$result <- ifelse(is.na(df$Winner), "no result",
ifelse(df$Team==df$Winner, "winner", "loser"))
df
Team Winner result
1 T1 T1 winner
2 T2 T1 loser
3 T2 <NA> no result
4 T3 <NA> no result
This is give NA where I want it to be losser
– Praveen Chougale
15 hours ago
Answer updated.
– Tim Biegeleisen
15 hours ago
add a comment |
up vote
1
down vote
Try this logic:
df$result <- ifelse(is.na(df$Winner), "no result",
ifelse(df$Team==df$Winner, "winner", "loser"))
df
Team Winner result
1 T1 T1 winner
2 T2 T1 loser
3 T2 <NA> no result
4 T3 <NA> no result
This is give NA where I want it to be losser
– Praveen Chougale
15 hours ago
Answer updated.
– Tim Biegeleisen
15 hours ago
add a comment |
up vote
1
down vote
up vote
1
down vote
Try this logic:
df$result <- ifelse(is.na(df$Winner), "no result",
ifelse(df$Team==df$Winner, "winner", "loser"))
df
Team Winner result
1 T1 T1 winner
2 T2 T1 loser
3 T2 <NA> no result
4 T3 <NA> no result
Try this logic:
df$result <- ifelse(is.na(df$Winner), "no result",
ifelse(df$Team==df$Winner, "winner", "loser"))
df
Team Winner result
1 T1 T1 winner
2 T2 T1 loser
3 T2 <NA> no result
4 T3 <NA> no result
edited 15 hours ago
answered 15 hours ago
Tim Biegeleisen
209k1380129
209k1380129
This is give NA where I want it to be losser
– Praveen Chougale
15 hours ago
Answer updated.
– Tim Biegeleisen
15 hours ago
add a comment |
This is give NA where I want it to be losser
– Praveen Chougale
15 hours ago
Answer updated.
– Tim Biegeleisen
15 hours ago
This is give NA where I want it to be losser
– Praveen Chougale
15 hours ago
This is give NA where I want it to be losser
– Praveen Chougale
15 hours ago
Answer updated.
– Tim Biegeleisen
15 hours ago
Answer updated.
– Tim Biegeleisen
15 hours ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407875%2fhow-to-write-ifelse-statement-with-multiple-conditions-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown