2017-02-21 48 views
0

我有以下數據刪除零和時間序列加入他們回來

library(xts) 
values<-c(2,2,2,4,2,3,0,0,0,0,0,1,2,3,2) 
time1<-seq(from=as.POSIXct("2013-01-01 00:00"),to=as.POSIXct("2013-01-1 14:00"),by="hour") 
data<-xts(values,order.by=time1) 
data 

    [,1] 
2013-01-01 00:00:00 2 
2013-01-01 01:00:00 2 
2013-01-01 02:00:00 2 
2013-01-01 03:00:00 4 
2013-01-01 04:00:00 2 
2013-01-01 05:00:00 3 
2013-01-01 06:00:00 0 
2013-01-01 07:00:00 0 
2013-01-01 08:00:00 0 
2013-01-01 09:00:00 0 
2013-01-01 10:00:00 0 
2013-01-01 11:00:00 1 
2013-01-01 12:00:00 2 
2013-01-01 13:00:00 3 
2013-01-01 14:00:00 2 

現在我想刪除所有的零,這可以很容易地

remove_zerro = apply(data, 1, function(row) all(row !=0)) 
data[remove_zerro,] 

實現的問題是,在我使用沒有零的數據並進行一些修改之後,我想在相同的日期和時間將零返回到我的數據。任何想法都會被認可

+0

用零和不用零分開你的數據,然後把它們放在一起,或者對子集data [data [,1]!= 0,] < - data [data [,1]!= 0進行「修改」 ,] + 1' – zx8754

回答

1

好像你可能想用稀疏向量/矩陣的工作:

install.packages("spam") 
library(spam) 
sx <- c(0,0,3, 3.2, 0,0,0,-3:1,0,0,2,0,0,5,0,0) 
apply.spam(spam(sx), NULL, function(x){1/x}) 
      [,1] 
[1,] 0.0000000 
[2,] 0.0000000 
[3,] 0.3333333 
[4,] 0.3125000 
[5,] 0.0000000 
[6,] 0.0000000 
[7,] 0.0000000 
[8,] -0.3333333 
[9,] -0.5000000 
[10,] -1.0000000 
[11,] 0.0000000 
[12,] 1.0000000 
[13,] 0.0000000 
[14,] 0.0000000 
[15,] 0.5000000 
[16,] 0.0000000 
[17,] 0.0000000 
[18,] 0.2000000 
[19,] 0.0000000 
[20,] 0.0000000 

如果零值做到了:

> apply(matrix(sx), 1, function(x){1/x}) 
[1]  Inf  Inf 0.3333333 0.3125000  Inf  Inf 
[7]  Inf -0.3333333 -0.5000000 -1.0000000  Inf 1.0000000 
[13]  Inf  Inf 0.5000000  Inf  Inf 0.2000000 
[19]  Inf  Inf 

所以你可以看到,apply.spam忽略零,但把它們放回自動

的缺點是,你必須重新安裝你的時間處理後標記回來。

0

我建立在@ zx8754的評論。

一種方法是拆分數據幀。如果您擔心會混淆索引或將數據框架連接在一起,那麼下面是一種替代方法。

創建一個T/F的索引。

idx <- df[,col] != 0 
df$col[idx] <- 2007 # or whatever operation. 
1

這裏有兩種可能的方法:

# re-create your data set 
library(xts) 
values<-c(2,2,2,4,2,3,0,0,0,0,0,1,2,3,2) 
time1<-seq(from=as.POSIXct("2013-01-01 00:00"),to=as.POSIXct("2013-01-1 14:00"),by="hour") 
data<-xts(values,order.by=time1) 
data 

############################################### 
# SOLUTION 1 : 
# make a union of the "zero" series and the "zero-free" series 

# create a copy of data with no zero 
isNotZero = apply(data, 1, function(row) all(row != 0)) 
zeroFreeSeries <- data[isNotZero,] 
zeroSeries <- data[!isNotZero,] 

# do you calculations on the "zero-free" series (e.g. add 10 to all values) 
zeroFreeSeries <- zeroFreeSeries + 10 

# union 
unionSeries <- rbind(zeroSeries,zeroFreeSeries) 

# now unionSeries contains what you desire 
unionSeries 

############################################### 
# SOLUTION 2 : 
# keep the original series copy and after doing your operations 
# on the "zero-free" series, update the original series copy with 
# with the new values (it doesn't work well if you remove some date from the 
# zero-free series) 

# create a copy of data with no zero 
isNotZero = apply(data, 1, function(row) all(row != 0)) 
zeroFreeSeries <- data[isNotZero,] 

# do you operations on the "zero-free" series (e.g. add 10 to all values) 
zeroFreeSeries <- zeroFreeSeries + 10 

# modify the original data by setting the new values 
data[time(zeroFreeSeries),] <- zeroFreeSeries 

# now data contains what you desire 
data 
+0

解決方案2正是我所期待的 – kelamahim

0

所以,很顯然這是解決方案

no<-data[ data[,1] != 0, ] #data without zeros 
yes<-data[ data[,1] == 0, ]# data with only zeros 

together<-c(no, yes)# both data combined together