In R how do I find whether an integer is divisible by a number?

心已入冬 提交于 2020-03-15 06:35:31

问题


Sorry for the inexperience, I'm a beginning coder in R For example: If I were to make a FOR loop by chance and I have a collection of integers 1 to 100 (1:100), what would be the proper format to ensure that would print the numbers that are divisible by another number. In this case, 5 being the number divisible. I hear that using modulo would help in this instance %%

This is what I think I should have.

For (x in 1:100) {
  x%%5 == y
}
print(y)

回答1:


for (x in 1:100) { 
  if (x%%5 == 0) {
    print(x)
  }
}



回答2:


How's this?

x <- 1:100
div_5 <- function(x) x[x %% 5 == 0]
div_5(x)



回答3:


for (i in 1:10) 
{
   if (i %% 2)
   {
      #some code
   }
}


来源:https://stackoverflow.com/questions/43359128/in-r-how-do-i-find-whether-an-integer-is-divisible-by-a-number

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