2012-01-18 123 views
4

我是R中的新人,真的不确定如何过滤日期帧中的数据。R - 从数据帧中过滤数据

我已经创建了包含月份日期和相应温度的两栏的数据框。它的长度为324.

> head(Nino3.4_1974_2000) 
    Month_common    Nino3.4_degree_1974_2000_plain 
1 1974-01-15      -1.93025 
2 1974-02-15      -1.73535 
3 1974-03-15      -1.20040 
4 1974-04-15      -1.00390 
5 1974-05-15      -0.62550 
6 1974-06-15      -0.36915 

过滤规则是选择大于或等于0.5度的温度。此外,它必须至少连续5个月。

我已经消除了温度低于0.5度的数据(见下文)。

for (i in 1) { 
el_nino=Nino3.4_1974_2000[which(Nino3.4_1974_2000$Nino3.4_degree_1974_2000_plain >= 0.5),] 
} 

> head(el_nino) 
    Month_common    Nino3.4_degree_1974_2000_plain 
32 1976-08-15      0.5192000 
33 1976-09-15      0.8740000 
34 1976-10-15      0.8864501 
35 1976-11-15      0.8229501 
36 1976-12-15      0.7336500 
37 1977-01-15      0.9276500 

但是,我仍然需要连续提取5个月。我希望有人能帮助我。

+0

是*永远*一个你本月'Month_common'行之间的区别是什么? – 2012-01-18 05:20:06

+0

是的,间距是一个月。 – 2012-01-18 05:25:04

回答

4

如果你总是可以依靠的间距是一个月内,然后让我们暂时抛却时间信息:

temps <- Nino3.4_1974_2000$Nino3.4_degree_1974_2000_plain 

所以,因为在每一个温度该向量是总是相隔一个月,我们只需要寻找temps[i]>=0.5的运行,并且运行必须至少5个长。

如果我们做到以下几点:

ofinterest <- temps >= 0.5 

我们只好值TRUE FALSE FALSE TRUE TRUE ....等载体ofinterest它的TRUEtemps[i]为> = 0.5和FALSE否则。

要重新解释您的问题,那么我们只需要查看的发生次数,连续至少有5个TRUE连续

要做到这一点,我们可以使用函数rle?rle给出:

> ?rle 
Description 
    Compute the lengths and values of runs of equal values in a vector 
    - or the reverse operation. 
Value: 
    ‘rle()’ returns an object of class ‘"rle"’ which is a list with 
    components:  
lengths: an integer vector containing the length of each run. 
    values: a vector of the same length as ‘lengths’ with the 
      corresponding values. 

因此我们使用rle它计算了所有的连续和连续TRUE条纹成一排连续FALSE,并连续寻找至少5 TRUE

我只是做了一些数据来证明:

# for you, temps <- Nino3.4_1974_2000$Nino3.4_degree_1974_2000_plain 
temps <- runif(1000) 

# make a vector that is TRUE when temperature is >= 0.5 and FALSE otherwise 
ofinterest <- temps >= 0.5 

# count up the runs of TRUEs and FALSEs using rle: 
runs <- rle(ofinterest) 

# we need to find points where runs$lengths >= 5 (ie more than 5 in a row), 
# AND runs$values is TRUE (so more than 5 'TRUE's in a row). 
streakIs <- which(runs$lengths>=5 & runs$values) 

# these are all the el_nino occurences. 
# We need to convert `streakIs` into indices into our original `temps` vector. 
# To do this we add up all the `runs$lengths` up to `streakIs[i]` and that gives 
# the index into `temps`. 
# that is: 
# startMonths <- c() 
# for (n in streakIs) { 
#  startMonths <- c(startMonths, sum(runs$lengths[1:(n-1)]) + 1 
# } 
# 
# However, since this is R we can vectorise with sapply: 
startMonths <- sapply(streakIs, function(n) sum(runs$lengths[1:(n-1)])+1) 

现在,如果你这样做Nino3.4_1974_2000$Month_common[startMonths]你会得到其中的厄尔尼诺开始的所有月份。

它归结为短短的几行:

runs <- rle(Nino3.4_1974_2000$Nino3.4_degree_1974_2000_plain>=0.5) 
streakIs <- which(runs$lengths>=5 & runs$values) 
startMonths <- sapply(streakIs, function(n) sum(runs$lengths[1:(n-1)])+1) 
Nino3.4_1974_2000$Month_common[startMonths] 
+0

谢谢,它很棒 – 2012-01-18 07:36:03

1

以下是一个使用事实的方法,即月份相隔一个月。比问题简化为找到连续5行与临时工> = 0.5度:

# Some sample data 
d <- data.frame(Month=1:20, Temp=c(rep(1,6),0,rep(1,4),0,rep(1,5),0, rep(1,2))) 
d 

# Use rle to find runs of temps >= 0.5 degrees 
x <- rle(d$Temp >= 0.5) 

# The find the last row in each run of 5 or more 
y <- x$lengths>=5 # BUG HERE: See update below! 
lastRow <- cumsum(x$lengths)[y] 

# Finally, deduce the first row and make a result matrix 
firstRow <- lastRow - x$lengths[y] + 1L 
res <- cbind(firstRow, lastRow) 
res 
#  firstRow lastRow 
#[1,]  1  6 
#[2,]  13  17 

UPDATE我有检测运行与5个值小于0.5太的错误。下面是更新后的代码(和测试数据):

d <- data.frame(Month=1:20, Temp=c(rep(0,6),1,0,rep(1,4),0,rep(1,5),0, 1)) 
x <- rle(d$Temp >= 0.5) 
y <- x$lengths>=5 & x$values 
lastRow <- cumsum(x$lengths)[y] 
firstRow <- lastRow - x$lengths[y] + 1L 
res <- cbind(firstRow, lastRow) 
res 
#  firstRow lastRow 
#[2,]  14  18 
+0

我不知道,它不能正常工作。特别是当数据凝视的数字小于.5时。 – 2012-01-18 06:51:49

+0

@YuDeng - 哎呀小错误。更新了答案。 – Tommy 2012-01-18 08:05:45

+0

谢谢,它运作良好, – 2012-01-19 00:10:59