r - Counting complete cases by ID for several variables -


i'm beginning learn r, apologies if simpler think is, i'm struggling find answer.

what i'm attempting create vector count of complete cases, id, multiple variables.

for example, in data frame:

id<-c(1:5) score.1<-c(1, 7, 3, 5, na, 4, 6, 9, 11, na) score.2<-c(2, na, 7, 6, na, 5, na, 7, 10, 1) sample<-data.frame(id, score.1, score.2)  id score.1 score.2    1       1       2   2       7      na   3       3       7   4       5       6   5      na      na   1       4       5   2       6      na   3       9       7   4      11      10   5      na       1 

the output i'm looking like:

id complete 1    4 2    2 3    4 4    4 5    1 

is there way i'm missing? i've tried count(complete.cases(sample)) plyr , sum(complete.cases()), it's not giving me want.

any appreciated.

you can use dplyr:

library(dplyr)  sample %>%    mutate(new_var = rowsums(!is.na(sample[,2:3]))) %>%     group_by(id) %>%    summarize(complete = sum(new_var)) 

the output looking for:

 id complete (int)    (dbl)   1        4   2        2   3        4   4        4   5        1 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -