Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retrieving number of observations with non missing values

Tags:

r

panel

I would like to compute the number of observations (Persons in my following example) that have non-missing values.

unbal <- data.frame(PERSON=c(rep('Frank',5),rep('Tony',5),rep('Edward',5)), YEAR=c(2001,2002,2003,2004,2005,2001,2002,2003,2004,2005,2001,2002,2003,2004,2005), Y=c(21,22,23,24,25,5,6,NA,7,8,31,32,33,34,35), X=c(1:15))
unbal

   PERSON YEAR  Y  X
1   Frank 2001 21  1
2   Frank 2002 22  2
3   Frank 2003 23  3
4   Frank 2004 24  4
5   Frank 2005 25  5
6    Tony 2001  5  6
7    Tony 2002  6  7
8    Tony 2003 NA  8
9    Tony 2004  7  9
10   Tony 2005  8 10
11 Edward 2001 31 11
12 Edward 2002 32 12
13 Edward 2003 33 13
14 Edward 2004 34 14
15 Edward 2005 35 15

In this case will be 2, since only two persons (Frank and Edward) have all the data.

like image 318
Economist_Ayahuasca Avatar asked Jan 01 '26 10:01

Economist_Ayahuasca


1 Answers

One way via base R,

sapply(split(unbal, unbal$PERSON), function(i) all(complete.cases(i)))
#Edward  Frank   Tony 
#  TRUE   TRUE  FALSE 

You can do this to extract,

ind <- sapply(split(unbal, unbal$PERSON), function(i) all(complete.cases(i)))
names(ind)[ind]
#[1] "Edward" "Frank" 

#or for the length
length(ind[ind])
#[1] 2
like image 139
Sotos Avatar answered Jan 03 '26 02:01

Sotos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!