有条件地选择dplyr中的列

时间:2018-10-25 09:22:21

标签: r select dplyr

library(nycflights13)
library(dplyr)

head(flights)

id.vec <- c("a", "b", "c")

for(i in seq_along(id.vec)){

  id <- id.vec[i]

  temp <- flights %>% dplyr::select(year, day, dep_time, arr_time) %>%
                      dplyr::mutate(year = year - mean(year),
                                    day = day - mean(day),
                                    dep_time = dep_time - mean(dep_time),
                                    arr_time = arr_time - mean(arr_time))

  # do some other tasks with temp file
}

我的问题是我该如何设置一个条件,如果id == "c",则不要 选择dep_time列,也不要执行dep_time = dep_time - mean(dep_time)

1 个答案:

答案 0 :(得分:0)

简单地说:(?)

for(i in seq_along(id.vec)){


  id <- id.vec[i]

  if(id == 'c') {
  temp <- flights %>% dplyr::select(year, day, arr_time) %>%
    dplyr::mutate(year = year - mean(year),
                  day = day - mean(day),
                  arr_time = arr_time - mean(arr_time))
  } else {
    temp <- flights %>% dplyr::select(year, day, dep_time, arr_time) %>%
      dplyr::mutate(year = year - mean(year),
                    day = day - mean(day),
                    dep_time = dep_time - mean(dep_time),
                    arr_time = arr_time - mean(arr_time))
  }

  # do some other tasks with temp file
}