Skip to content

count NAs per week

8 messages · R. Michael Weylandt, William Dunlap, arun +2 more

#
Even though I work with R since a year or so I still struggle with simple
problems. I hope someone can help me with this. Been trying for days and am
a little frustrated now. 

I have a data frame somewhat like the one bellow:

dattrial<-data.frame(a=c(1,NA,rnorm(4,10)), Week=c(3,3,3,4,4,4))

I want to know how many NAs I have in week 3 and in week 4. 



--
View this message in context: http://r.789695.n4.nabble.com/count-NAs-per-week-tp4643351.html
Sent from the R help mailing list archive at Nabble.com.
#
On Mon, Sep 17, 2012 at 11:03 AM, Tagmarie <Ramgad82 at gmx.net> wrote:
Thanks for the reproducible example: there are many ways to do this
(aggregate, tapply, ave, etc.) but they are all based on the paradigm
of: break up your data by "Week" --> apply the function "function(x)
sum(is.na(x))" --> recombine. (See, inter alia, the JSS paper on the
plyr package) I'm not at a computer with R right now so this is a
little untested, but one way might be:

with(dattrial, tapply(a, Week, function(x) sum(is.na(x))))

Cheers,
Michael
#
Hi,
Try this:
dattrial<-data.frame(a=c(1,NA,rnorm(4,10)), Week=c(3,3,3,4,4,4))
?aggregate(dattrial$a,list(dattrial$Week),function(x) sum(is.na(x)))
#? Group.1 x
#1?????? 3 1
#2?????? 4 0
#or
?ddply(dattrial,.(Week),summarize, sum(is.na(a))) 
#? Week ..1
#1??? 3?? 1
#2??? 4?? 0

#or
list1<-split(dattrial,dattrial$Week)
?unlist(lapply(lapply(list1,`[`, 1),function(x) sum(is.na(x))))
#3 4 
#1 0 
A.K. 





----- Original Message -----
From: Tagmarie <Ramgad82 at gmx.net>
To: r-help at r-project.org
Cc: 
Sent: Monday, September 17, 2012 6:03 AM
Subject: [R] count NAs per week

Even though I work with R since a year or so I still struggle with simple
problems. I hope someone can help me with this. Been trying for days and am
a little frustrated now. 

I have a data frame somewhat like the one bellow:

dattrial<-data.frame(a=c(1,NA,rnorm(4,10)), Week=c(3,3,3,4,4,4))

I want to know how many NAs I have in week 3 and in week 4. 



--
View this message in context: http://r.789695.n4.nabble.com/count-NAs-per-week-tp4643351.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
#
Thank you Michael, that worked perfectly! 

Now I wonder, if it is possible to break my data further apart and put it
together again. 
Assume I include a column for an ID in the data frame like this: 

dattrial2<-data.frame(a=c(1,NA,NA,NA,2,3), Week=c(3,3,3,4,4,4),
AnimalID=c("Ernie","Bert", "Ernie", "Bert", "Bert", "Ernie"))

Is it possible to get two different lists in the output, one for Ernie and
one for Bert? Or do I have to do it seperately for each animal?

Thank you again! I learn a lot by doing and by people helping me. Thank you
for the hint with the paper. 





--
View this message in context: http://r.789695.n4.nabble.com/count-NAs-per-week-tp4643351p4643371.html
Sent from the R help mailing list archive at Nabble.com.
#
Week
A_is_missing 3 4
       FALSE 2 3
       TRUE  1 0
3 4 
1 0

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
#
HI,
May be this is what you wanted.
?res1<-aggregate(dattrial2$a,list(dattrial2$Week,dattrial2$AnimalID),function(x) sum(is.na(x)))
#$Bert
#? Group.1 Group.2 x
#1?????? 3??? Bert 1
#2?????? 4??? Bert 1

#$Ernie
?# Group.1 Group.2 x
#3?????? 3?? Ernie 1
#4?????? 4?? Ernie 0


A.K.



----- Original Message -----
From: Tagmarie <Ramgad82 at gmx.net>
To: r-help at r-project.org
Cc: 
Sent: Monday, September 17, 2012 9:07 AM
Subject: Re: [R] count NAs per week

Thank you Michael, that worked perfectly! 

Now I wonder, if it is possible to break my data further apart and put it
together again. 
Assume I include a column for an ID in the data frame like this: 

dattrial2<-data.frame(a=c(1,NA,NA,NA,2,3), Week=c(3,3,3,4,4,4),
AnimalID=c("Ernie","Bert", "Ernie", "Bert", "Bert", "Ernie"))

Is it possible to get two different lists in the output, one for Ernie and
one for Bert? Or do I have to do it seperately for each animal?

Thank you again! I learn a lot by doing and by people helping me. Thank you
for the hint with the paper. 





--
View this message in context: http://r.789695.n4.nabble.com/count-NAs-per-week-tp4643351p4643371.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
#
On Sep 17, 2012, at 6:07 AM, Tagmarie wrote:

            
split(dattrial2, dattrial2[['AnimalID']])
David Winsemius, MD
Alameda, CA, USA
#
Thank you Arun, that was exactly what I was looking for. 
This is the second time you helped me in this mailing list. Thanks!

And also thanks to all of the others of you whou helped me. It is amazing
how many possibilities there are to solve my problem (which isn't a prob now
anymore). 
Have a great day, 
Tagmarie 



--
View this message in context: http://r.789695.n4.nabble.com/count-NAs-per-week-tp4643351p4643486.html
Sent from the R help mailing list archive at Nabble.com.