#R-FOR循环-如何循环遍历data.frame列中向量的每个元素

时间:2018-06-21 21:48:56

标签: r for-loop

目标是创建一个向量,指示学生是否缺席(1),出席(0)或数据是否丢失(NA)

Present <- data$attendancecode
attendance <- c()

for (i in seq_along(Present)){
  if (is.na(i)==TRUE) {
    attendance [i] <- NA
  } else if (grepl("A|G|X|Z", i)){
    attendance [i] <- 1
  } else {attendance [i] <- 0}
}

不确定为什么这不起作用...

2 个答案:

答案 0 :(得分:2)

seq_along将为您提供索引向量,即1:length(Present)。因此,您的is.na调用将始终返回false。代替

for (i in seq_along(Present)){
  if (is.na(present[i])) { # don't need == TRUE, is.na returns TRUE or FALSE
    attendance [i] <- NA
  } else if (grepl("A|G|X|Z", i)){
    attendance [i] <- 1
  } else {Present [i] <- 0} # not sure what you intend to do here... do you mean to overwrite the value in Present? or do you mean to assign to attendance?
}

答案 1 :(得分:0)

我想在您的最后一行中,当您打算在Present中进行分配时,您在attendance中进行了分配。但是我认为@mikeck才是真正的问题。

也就是说,如果您不使用for循环就可以了:

Present <- c("A", "G", "Z", "Q", "Q", NA, NA)

attendance <- purrr::map_dbl(Present, ~case_when(
  is.na(.x) ~ NA_real_,
  grepl("A|G|X|Z", .x) ~ 1,
  TRUE ~ 0
))

> attendance
[1]  1  1  1  0  0 NA NA