【问题标题】:Finding first occurrence of value by year按年份查找第一次出现的值
【发布时间】:2013-07-20 15:26:37
【问题描述】:

我有几个监测点的降水数据集:

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))

我需要在每个站点中找到每年第一次出现高于 0 的任何值。我不知道该怎么做。结果应该是这样的[数据框很好]:

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 

如何在 R 中做到这一点?

谢谢, J.

【问题讨论】:

  • 请提供reproducible example 说明您尝试过的内容。并查看FAQ on how to ask a good question
  • 还可以添加预期的输出吗?是c(2010-01-01,0.7)吗?
  • 请重新阅读 Joshua 的评论和他提供的链接。
  • 我还应该提供什么?我附上了数据示例和我的答案想法......
  • 当您提供示例数据时,您应该使用dput。您还应该展示您尝试过的内容。你希望输出是什么形式的?那是data.frame 吗?它看起来不像 xts

标签: r time-series xts


【解决方案1】:

这个怎么样?

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))))

应该输出哪个(假设行按日期排序):

     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

【讨论】:

  • 非常感谢,它工作得很好(在普通数据框上)!我还有一个问题——如何在 xts 对象上使用它?
  • 我猜也是... library(xts); my.first &lt;- function(x) head(names(x)[x],1); data(sample_matrix); year &lt;- format(as.Date(rownames(sample_matrix)),"%Y"); sample.xts &lt;- as.xts(sample_matrix); do.call("rbind",by(sample.xts&gt;50,year, function(x) apply(x,2,my.first))) ...(在这种情况下找到值> 50)
  • 这就够了:my.first 0,year, function (x) 申请(x,2,my.first))) 谢谢!
猜你喜欢
  • 1970-01-01
  • 2018-11-28
  • 1970-01-01
  • 1970-01-01
  • 2011-10-04
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
相关资源
最近更新 更多