【问题标题】:Using the which operator, find the matches where his score was an even number, and print the match numbers使用 which 运算符,找到他的分数为偶数的匹配项,并打印匹配项
【发布时间】:2019-06-18 13:21:52
【问题描述】:

Virat 在最近的 10 场测试比赛中得分 98,102,120,145,175,169,118,177,101,200 次。使用 which 运算符,找到他的分数为偶数的匹配项,并打印匹配项。

我试过下面的代码:

Virat_Scored  <- c(98,102,120,145,175,169,118,177,101,200)
a <- Virat_Scored %% 2 == 0
print(Virat_Scored[a])

上述问题的正确或准确代码是什么。

【问题讨论】:

  • 你可以做Virat_Scored[Virat_Scored %% 2 == 0]。
  • 有没有哪个算子可以解决这个问题
  • Virat_Scored[which(Virat_Scored %% 2 == 0)]

标签: r vector


【解决方案1】:

使用which获取偶数的索引

ind = which(Virat_Scored %% 2 == 0)

然后从 Virat_Scored 向量中提取这些

Virat_Scored[ind] 
[1]  98 102 120 118 200

【讨论】:

    【解决方案2】:

    下面对我有用:)

    Virat_Scored  <- c(98,102,120,145,175,169,118,177,101,200)
    which(Virat_Scored %% 2 == 0)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-17
      • 1970-01-01
      • 2015-04-07
      • 1970-01-01
      • 2019-05-10
      • 2019-12-17
      • 2017-01-06
      • 1970-01-01
      相关资源
      最近更新 更多