Home > Software engineering >  replace for loop with dplyr
replace for loop with dplyr

Time:03-22

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

CodePudding user response:

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
  • Related