0

I have a dataset with precipitation data in few monitoring sites:

structure(list(date = structure(1:10, .Label = c("2010-01-01", 
"2010-01-02", "2010-01-03", "2010-01-04", "2010-01-05", "2010-01-06", 
"2010-01-07", "2010-01-08", "2011-01-01", "2011-01-02"), class = "factor"), 
    site1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), site2 = c(0.7, 0, 
    1.4, 0, 0, 0, 2.2, 0, 0, 2.2), site3 = c(0, 0, 0, 0, 0, 1.3, 
    0.6, 0, 1.3, 0.6), site4 = c(0L, 0L, 0L, 0L, 0L, 0L, 2L, 
    0L, 0L, 2L), site5 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), site6 = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), site7 = c(0, 0, 0, 1, 0, 4, 3, 
    1, 4, 3), site8 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("date", 
"site1", "site2", "site3", "site4", "site5", "site6", "site7", 
"site8"), class = "data.frame", row.names = c(NA, -10L))

I need to find first occurrence of any value higher than 0 in every year at every sites. I have no idea how to do it. Result should look like this [data frame is good]:

year    site1  site2  site3  site4  site5  site6  site7  site8
2001    01-02  01-02  01-02  01-02  01-01  02-02  01-01  01-02 
2002    01-03  01-02  02-02  01-02  01-02  01-03  01-02  04-02 
2003    01-02  01-05  01-02  01-02  05-02  01-02  01-07  01-02 
2004    05-02  01-02  01-02  07-02  01-02  05-02  01-02  01-06 

How to do that in R?

Thanks, J.

Jot eN
  • 6,120
  • 4
  • 40
  • 59
  • 2
    Please provide a [reproducible example](http://stackoverflow.com/q/5963269/271616) of what you've tried. And see the [FAQ on how to ask a good question](http://stackoverflow.com/help/how-to-ask). – Joshua Ulrich Jul 20 '13 at 15:30
  • Can also add the expected output? Is it `c(2010-01-01,0.7)`? – agstudy Jul 20 '13 at 16:06
  • 1
    Please re-read Joshua's comment and the links he provided. – GSee Jul 20 '13 at 16:18
  • What else should I provide? I attached example of the data and my idea of an answer... – Jot eN Jul 20 '13 at 16:48
  • when you provide example data, you should use `dput`. You should also show what you've tried. And what form do you expect that output to be in? Is that a `data.frame`? It doesn't look like an `xts`. – GSee Jul 20 '13 at 16:52
  • Thanks for advices. Is it good now? – Jot eN Jul 20 '13 at 17:01

1 Answers1

2

How about this?

mydata <- read.table(text="date site1 site2 site3 site4 site5 site6 site7 site8
2010-01-01 0.0 0.7 0.0 0 0.0 0.0 0.0 0.0
2010-01-02 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0
2010-01-03 0.0 1.4 0.0 0 0.0 0.0 0.0 0.0
2010-01-04 0.0 0.0 0.0 0 0.0 0.0 1.0 0.0
2010-01-05 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0
2010-01-06 0.0 0.0 1.3 0 0.0 0.0 4.0 0.0
2010-01-07 0.0 2.2 0.6 2 0.0 0.0 3.0 0.0
2010-01-08 0.0 0.0 0.0 0 0.0 0.0 1.0 0.0
2011-01-01 0.0 0.0 1.3 0 0.0 0.0 4.0 0.0
2011-01-02 0.0 2.2 0.6 2 0.0 0.0 3.0 0.0",h=T)

year <- format(as.Date(mydata$date),"%Y")
rownames(mydata) <- mydata[,1]
my.first <- function(x) head(names(x)[x],1)
do.call("rbind",(by(mydata[,-1]>0,year, function(x) apply(x,2,my.first))))

Which should output (assuming rows are ordered by date):

     site1       site2        site3        site4        site5       site6       site7        site8      
2010 Character,0 "2010-01-01" "2010-01-06" "2010-01-07" Character,0 Character,0 "2010-01-04" Character,0
2011 Character,0 "2011-01-02" "2011-01-01" "2011-01-02" Character,0 Character,0 "2011-01-01" Character,0
texb
  • 547
  • 2
  • 13
  • Thanks a lot, It's working great (on normal data frame)! And I have one more question - how can I use it on xts object? – Jot eN Jul 20 '13 at 16:28
  • I guess likewise along the lines of ... `library(xts); my.first <- function(x) head(names(x)[x],1); data(sample_matrix); year <- format(as.Date(rownames(sample_matrix)),"%Y"); sample.xts <- as.xts(sample_matrix); do.call("rbind",by(sample.xts>50,year, function(x) apply(x,2,my.first)))` ...(in this case finding values >50) – texb Jul 20 '13 at 16:41
  • It was enough: my.first <- function(x) head(names(x)[x],1) do.call("rbind",by(aln_miastax>0,year, function(x) apply(x,2,my.first))) Thank You! – Jot eN Jul 20 '13 at 16:46