R for循环会忽略条件if语句

时间:2019-06-06 15:11:18

标签: r for-loop if-statement conditional-statements na

我正在处理一个数据集,如果值不丢失,我需要R跳过它。我尝试过创建一个for循环,但是R忽略了我的逻辑。我见过其他for循环帖子,但它们不涉及被忽略的条件。

这是一个示例数据集:

library(dplyr)
  my_problem <- tibble(name = c("Joe", "Joseph", "Joey"),
                       score1 = c(2, 7, 12),
                       score2 = c(NA, 5, 10))

这就是我想要的样子:

solution <- tibble(name = c("Joe", "Joseph", "Joey"),
                     score1 = c(1, 7, 12),
                     score2 = c(NA, 5, 10),
                     score2edit = c(.30103, 5, 10))

这是我的for循环,如果score2列为NA,则对score1进行log10()转换。但是,由于某种原因,代码会忽略我的if语句,而直接跳转到else。

  for(i in 1:nrow(my_problem)) {
    if(is.na(my_problem$score2[i])) {
      my_problem$score2edit <- log10(my_problem$score1)
    } else {
      my_problem$score2edit <- my_problem$score2
    }
  }

谢谢!如果您还可以解释为什么此循环不起作用,那将非常有帮助。

1 个答案:

答案 0 :(得分:3)

我们可以使用向量化选项(C398C2B4C399C284C398C2A7C398C2A4

ifelse/if_else/case_when

library(dplyr) my_problem %>% mutate(score2edit = case_when(is.na(score2) ~ log10(score1), TRUE ~ score2)) # A tibble: 3 x 4 # name score1 score2 score2edit # <chr> <dbl> <dbl> <dbl> #1 Joe 2 NA 0.301 #2 Joseph 7 5 5 #3 Joey 12 10 10 循环遍历每一行,因此,如果使用它,然后对整个数据集进行赋值/替换,则会在每一行中替换整列

相关问题