lundi 20 juillet 2020

Selecting rows with multiple if and if else statements (R)

I tried to solve the following problem with if and else if statements:

  1. If "TRUE1" is apparent in column "check" select rows with "TRUE1"
  2. If "TRUE1 is not apparent in column "check" select rows with "TRUE2" else rows with "TRUE3"

The below code seems to work fine when "TRUE1" and "TRUE2" are available in column "check":

name <- c(1, 2, 3, 4, 5)
check <- c("TRUE1", "TRUE2", "TRUE3", "TRUE3", "TRUE3")
dataset <- data.frame(cbind(name, check))

> dataset
  name check
1    1 TRUE1
2    2 TRUE2
3    3 TRUE3
4    4 TRUE3
5    5 TRUE3

slct_set <- if (dataset$check == "TRUE1") {
  dataset[dataset[, "check"] == "TRUE1",] 
} else if (dataset$check != "TRUE1") {
  dataset[dataset[, "check"] == "TRUE2",]
} else {
  dataset[dataset[, "check"] == "TRUE3",]
}

> slct_set
  name check
1    1 TRUE1

However, when I use "TRUE3" for the whole column "check" this happens:

> dataset
  name check
1    1 TRUE3
2    2 TRUE3
3    3 TRUE3
4    4 TRUE3
5    5 TRUE3

> slct_set <- slct_set <- if (dataset$check == "TRUE1") {
  dataset[dataset[, "check"] == "TRUE1",] 
} else if (dataset$check != "TRUE1") {
  dataset[dataset[, "check"] == "TRUE2",]
} else {
  dataset[dataset[, "check"] == "TRUE3",]
}

Warning messages:
1: In if (dataset$check == "TRUE1") dataset[dataset[, "check"] == "TRUE1",  :
  the condition has length > 1 and only the first element will be used
2: In if (dataset$check != "TRUE1") dataset[dataset[, "check"] == "TRUE2",  :
  the condition has length > 1 and only the first element will be used

> slct_set
[1] name  check
<0 Zeilen> (oder row.names mit Länge 0)

I am quite new to if statements in R, so any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire