简体   繁体   中英

replace for loop with dplyr

I would like to replace this simple for loop with a dplyr option:

df <- data.frame(ID=c(rep(7,3), rep(8,3), rep(9,3)), visit= c(rep(c(0, 180, 360),3)), value = c("Poor", NA, "High", "High", NA, "High", NA, "Poor", "Poor") )

vec <- sum(!is.na(df$value)[df$visit==0])

for (i in seq(180, 360, 180)) {
      tmp <- sum(!is.na(df$value)[df$visit==i])
      vec <- c(vec, tmp)
 }

to obtain the vector of lengths where 'value' is not missing, for each 'visit' i :

 vec
[1] 2 1 3

A simple dplyr approach would be group_by your visit column, then sum up value that is not NA .

If you wish you have a dataframe instead of a vector, just ignore pull(vec) .

library(dplyr)

df %>% 
  group_by(visit) %>% 
  summarize(vec = sum(!is.na(value))) %>% 
  pull(vec)

[1] 2 1 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM