Subsetting a vector using multiple conditions

岁酱吖の 提交于 2019-12-24 08:03:38

问题


> 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!