问题
> which(LETTERS=="A")
[1] 1
> which(LETTERS=="B")
[1] 2
Can i use one statement in which to get the value of 1,2?
which(LETTERS=="B" or "A")
Error: unexpected symbol in "which(LETTERS=="B" or"
回答1:
which(LETTERS == "A" | LETTERS == "B")
Or:
which(LETTERS %in% c("A", "B"))
回答2:
as answer for the title, how subsetting a vector using multiple conditions:
# Random Data
test1 <- sample(x = c(1:30),size = 15,replace = TRUE)
test2 <- sample(x = c(70:75),size = 15,replace = TRUE)
test3 <- sample(x = c(1:100),size = 15,replace = TRUE)
# actual data
# test1
# [1] 19 1 9 6 15 1 16 4 1 10 11 19 24 1 17
# test2
# [1] 71 72 70 74 71 74 74 75 73 73 72 74 74 73 71
# test3
# [1] 24 95 66 8 9 97 85 1 40 55 37 84 95 93 95
# subsetting vector 3 with multiple conditons
test3[test1 > test3 & test2 < 5 * test1]
# [1] 9
来源:https://stackoverflow.com/questions/12548744/subsetting-a-vector-using-multiple-conditions