Tagmarie
2012-Jun-16 20:11 UTC
[R] count data without NA in certain time intervals and plot it
Hello, I'm quite new to R and still spend hours trying to figure out single things so I hope nobody rolls his eyes over my question. I have a data set over time and converted it to the POSTIXct format. I added a column in the data set for the week and the month. I try to get a plot which shows the weeks on the x-axis and the number of datasets without NAs on the y-axis. That doesn't sound too difficult but I can't figure it out. Does anybody have an idea? -- View this message in context: http://r.789695.n4.nabble.com/count-data-without-NA-in-certain-time-intervals-and-plot-it-tp4633611.html Sent from the R help mailing list archive at Nabble.com.
arun
2012-Jun-17 01:36 UTC
[R] count data without NA in certain time intervals and plot it
Hi, Not quite understand the question. Do you want to select only certain columns or rows without NAs? Suppose, I have a dataset such as the one below: dattrial<-data.frame(a=c(1,NA,rnorm(4,10)),b=c(NA,NA,NA,3,4,6),c=c(sample(LETTERS[1:3],replace=TRUE), sample(LETTERS[3:5],3,replace=TRUE)),d=runif(6,0.4)) # to eliminate the rows with NAs dattrial1<-dattrial[complete.cases(dattrial),] # to delete columns with NAs dattrial1<-dattrial[,colSums(is.na(dattrial))==0] or dattrial1<-dattrial[rowSums(is.na(dattrial))==0,] A.K. ----- Original Message ----- From: Tagmarie <Ramgad82 at gmx.net> To: r-help at r-project.org Cc: Sent: Saturday, June 16, 2012 4:11 PM Subject: [R] count data without NA in certain time intervals and plot it Hello, I'm quite new to R and still spend hours trying to figure out single things so I hope nobody rolls his eyes over my question. I have a data set over time and converted it to the POSTIXct format. I added a column in the data set for the week and the month. I try to get a plot which shows the weeks on the x-axis and the number of datasets without NAs on the y-axis. That doesn't sound too difficult but I can't figure it out. Does anybody have an idea? -- View this message in context: http://r.789695.n4.nabble.com/count-data-without-NA-in-certain-time-intervals-and-plot-it-tp4633611.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.
Tagmarie
2012-Jun-17 09:40 UTC
[R] count data without NA in certain time intervals and plot it
Thank you Arun for your time! Your idea is maybe only the first step to what I want but it was nevertheless a new tool for me and interessing to learn. I added a "week"-column to your data set: dattrial<-data.frame(a=c(1,NA,rnorm(4,10)), Week=c(3,3,3,4,4,4)) I am looking for a way to count the number of rows for each week which do contain data (without NA). In the next step I want to create a graph which shows the week on the x-axis and the counted number of data for each week on the y-axis. Thank you! -- View this message in context: http://r.789695.n4.nabble.com/count-data-without-NA-in-certain-time-intervals-and-plot-it-tp4633611p4633635.html Sent from the R help mailing list archive at Nabble.com.
Rui Barradas
2012-Jun-17 10:12 UTC
[R] count data without NA in certain time intervals and plot it
Hello,
I've seen your reply to arun's reply and gave it a try.
Since arun's code included more than one column, I've added another in
one of the examples.
# Example 1
dattrial1 <- data.frame(a=c(1,NA,rnorm(4,10)), Week=c(3,3,3,4,4,4))
d1 <- split(dattrial1, dattrial1$Week)
count <- sapply(d1, function(x) sum(!is.na(x$a)))
count
# Example 2
dattrial2 <- data.frame(a=c(1,NA,rnorm(4,10)), b=c(1,2,NA,3,4,6),
Week=c(3,3,3,4,4,4))
d2 <- split(dattrial2, dattrial2$Week)
count <- sapply(d2, function(x){
yes <- apply(x, 1, function(y) all(!is.na(y)))
sum(yes)
})
count
# Works for both examples
plot(names(count), count, type="b", col="red", pch=16)
Hope this helps,
Rui Barradas
Em 16-06-2012 21:11, Tagmarie escreveu:> Hello,
> I'm quite new to R and still spend hours trying to figure out single
things
> so I hope nobody rolls his eyes over my question.
>
> I have a data set over time and converted it to the POSTIXct format. I
added
> a column in the data set for the week and the month.
>
> I try to get a plot which shows the weeks on the x-axis and the number of
> datasets without NAs on the y-axis. That doesn't sound too difficult
but I
> can't figure it out.
>
> Does anybody have an idea?
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/count-data-without-NA-in-certain-time-intervals-and-plot-it-tp4633611.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.
>