2016-02-12 51 views
0

我有以下數據集:10分鐘實例取自顯示給定時間站內自行車數量的饋送。我的目標是要找出從一個時間的值差到下一個每個station_id循環顯示連續列之間的值差異

 
> dim(sample) 
[1] 6 73 
> sample 
    station_id 2014-08-18 20:00:00 2014-08-18 20:10:00 2014-08-18 20:20:00 
1   1     1     0     0 
2   2     18     18     19 
3   3     5     4     4 
4   4     21     20     20 
5   5     9     10     8 
6   6     9     9     9 
    2014-08-18 20:30:00 2014-08-18 20:40:00 2014-08-18 20:50:00 2014-08-18 21:00:00 
1     2     1     1     0 
2     18     18     21     22 
3     4     4     4     4 
4     21     22     25     23 
5     5     7     3     0 
6     9     9     9     9 
    2014-08-18 21:10:00 2014-08-18 21:20:00 2014-08-18 21:30:00 2014-08-18 21:40:00 
1     0     4     2     1 
2     21     22     22     21 
3     4     6     3     3 
4     25     24     24     24 
5     0     0     0     0 
6     9     9     10     10 
    2014-08-18 21:50:00 2014-08-18 22:00:00 2014-08-18 22:10:00 2014-08-18 22:20:00 
1     3     2     3     2 
2     21     22     18     16 
3     4     5     6     4 
4     25     22     21     21 
5     4     0     0     3 
6     10     10     11     11 
    2014-08-18 22:30:00 2014-08-18 22:40:00 2014-08-18 22:50:00 2014-08-18 23:00:00 
1     2     3     8     11 
2     15     14     16     17 
3     4     4     4     4 
4     23     21     20     20 
5     0     1     0     0 
6     11     12     12     12 
    2014-08-18 23:10:00 2014-08-18 23:20:00 2014-08-18 23:30:00 2014-08-18 23:40:00 
1     11     13     14     15 
2     17     15     10     3 
3     5     5     5     4 
4     22     19     19     17 
5     1     0     2     3 
6     12     12     12     12 
    2014-08-18 23:50:00 2014-08-19 00:00:00 2014-08-19 00:10:00 2014-08-19 00:20:00 
1     16     16     17     19 
2     2     2     2     1 
3     5     6     5     5 
4     18     19     19     20 
5     3     0     2     6 
6     12     12     12     12 
    2014-08-19 00:30:00 2014-08-19 00:40:00 2014-08-19 00:50:00 2014-08-19 01:00:00 
1     20     21     22     23 
2     1     4     3     3 
3     3     3     3     3 
4     20     19     20     19 
5     0     1     2     5 
6     12     12     12     12 
    2014-08-19 01:10:00 2014-08-19 01:20:00 2014-08-19 01:30:00 2014-08-19 01:40:00 
1     22     23     23     21 
2     3     4     4     4 
3     4     4     4     4 
4     21     24     23     24 
5     0     3     2     1 
6     12     12     12     12 
    2014-08-19 01:50:00 2014-08-19 02:00:00 2014-08-19 02:10:00 2014-08-19 02:20:00 
1     22     23     22     22 
2     3     1     1     1 
3     4     4     4     4 
4     24     22     21     22 
5     0     1     2     2 
6     12     12     12     12 
    2014-08-19 02:30:00 2014-08-19 02:40:00 2014-08-19 02:50:00 2014-08-19 03:00:00 
1     20     22     22     22 
2     1     1     1     1 
3     4     4     4     4 
4     22     24     24     23 
5     2     3     3     3 
6     12     12     12     12 
    2014-08-19 03:10:00 2014-08-19 03:20:00 2014-08-19 03:30:00 2014-08-19 03:40:00 
1     22     22     21     21 
2     1     1     1     1 
3     4     4     4     4 
4     23     23     23     22 
5     4     3     3     3 
6     12     12     12     12 
    2014-08-19 03:50:00 2014-08-19 04:00:00 2014-08-19 04:10:00 2014-08-19 04:20:00 
1     21     21     21     21 
2     1     1     1     1 
3     4     4     4     4 
4     22     22     23     23 
5     2     3     3     3 
6     12     12     12     12 
    2014-08-19 04:30:00 2014-08-19 04:40:00 2014-08-19 04:50:00 2014-08-19 05:00:00 
1     21     21     21     21 
2     1     1     1     1 
3     4     4     4     4 
4     23     23     23     23 
5     5     4     4     4 
6     12     12     12     12 
    2014-08-19 05:10:00 2014-08-19 05:20:00 2014-08-19 05:30:00 2014-08-19 05:40:00 
1     21     21     21     21 
2     1     1     1     1 
3     4     4     4     4 
4     24     24     24     24 
5     4     4     4     4 
6     12     12     12     12 
    2014-08-19 05:50:00 2014-08-19 06:00:00 2014-08-19 06:10:00 2014-08-19 06:20:00 
1     21     21     21     22 
2     1     1     1     1 
3     4     4     4     4 
4     24     24     24     24 
5     4     4     4     3 
6     12     12     12     12 
    2014-08-19 06:30:00 2014-08-19 06:40:00 2014-08-19 06:50:00 2014-08-19 07:00:00 
1     22     22     22     22 
2     1     1     1     1 
3     5     5     5     5 
4     24     24     24     24 
5     3     4     4     4 
6     12     12     12     12 
    2014-08-19 07:10:00 2014-08-19 07:20:00 2014-08-19 07:30:00 2014-08-19 07:40:00 
1     22     22     22     22 
2     6     5     5     5 
3     5     5     5     5 
4     24     24     24     24 
5     4     4     4     4 
6     12     12     12     12 
    2014-08-19 07:50:00 
1     22 
2     5 
3     5 
4     24 
5     4 
6     12 

最後出來放應該是這個樣子(當然,列 實際數量自然會73,但是放入這個例子太多了)。

 
> reshape2 
    station_id 2014-08-18 20:00:00 2014-08-18 20:10:00 2014-08-18 20:20:00 
1   1     1     -1     0 
2   2     18     0     1 
3   3     5     -1     0 
4   4     21     -1     0 
5   5     9     1     -2 
6   6     9     0     0 
    2014-08-18 20:30:00 2014-08-18 20:40:00 
1     2     -1 
2     -1     0 
3     0     0 
4     1     1 
5     -3     2 
6     0     0 

如何循環遍歷R中的所有列?

下面是數據

 
> dput(sample) 
structure(list(station_id = 1:6, `2014-08-18 20:00:00` = c(1L, 
18L, 5L, 21L, 9L, 9L), `2014-08-18 20:10:00` = c(0L, 18L, 4L, 
20L, 10L, 9L), `2014-08-18 20:20:00` = c(0L, 19L, 4L, 20L, 8L, 
9L), `2014-08-18 20:30:00` = c(2L, 18L, 4L, 21L, 5L, 9L), `2014-08-18 20:40:00` = c(1L, 
18L, 4L, 22L, 7L, 9L), `2014-08-18 20:50:00` = c(1L, 21L, 4L, 
25L, 3L, 9L), `2014-08-18 21:00:00` = c(0L, 22L, 4L, 23L, 0L, 
9L)), .Names = c("station_id", "2014-08-18 20:00:00", "2014-08-18 20:10:00", "2014-08-18 20:20:00", "2014-08-18 20:30:00", "2014-08-18 20:40:00", "2014-08-18 20:50:00", "2014-08-18 21:00:00"), row.names = c(NA, 
6L), class = "data.frame") 
+0

請考慮在讀了[問]以及如何生成[可重現的示例](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。請儘量讓您的示例儘可能少且易於導入。此外,嘗試展示一些你自己的嘗試來解決這個問題。你有什麼嘗試?爲什麼它不起作用? – Heroka

+0

我已經包含上述數據的樣本 –

+0

謝謝!現在,你是否也可以根據這個輸入包含所需的輸出,並且你自己試圖解決這個問題? – Heroka

回答

0

如何像:

t(apply(sample[,-1], 1, diff)) 
0

如果這是你想要一個循環的一個小樣本,一種方法是下面。我敢肯定,有更優雅的方式....

> set.seed(100) 
> df = data.frame(id = 1:10, replicate(8,sample(0:1,10,rep=TRUE))) 
> df 
    id X1 X2 X3 X4 X5 X6 X7 X8 
1 1 0 1 1 0 0 0 0 0 
2 2 0 1 1 1 1 0 1 0 
3 3 1 0 1 0 1 0 1 1 
4 4 0 0 1 1 1 0 1 1 
5 5 0 1 0 1 1 1 0 1 
6 6 0 1 0 1 0 0 0 1 
7 7 1 0 1 0 1 0 0 1 
8 8 0 0 1 1 1 0 0 1 
9 9 1 0 1 1 0 1 0 1 
10 10 0 1 0 0 0 0 1 0 
> df1 = df 
> for(i in 3:ncol(df1)) df1[,i] = df[,i]-df[,(i-1)] 
> df1 
    id X1 X2 X3 X4 X5 X6 X7 X8 
1 1 0 1 0 -1 0 0 0 0 
2 2 0 1 0 0 0 -1 1 -1 
3 3 1 -1 1 -1 1 -1 1 0 
4 4 0 0 1 0 0 -1 1 0 
5 5 0 1 -1 1 0 0 -1 1 
6 6 0 1 -1 1 -1 0 0 1 
7 7 1 -1 1 -1 1 -1 0 1 
8 8 0 0 1 0 0 -1 0 1 
9 9 1 -1 1 0 -1 1 -1 1 
10 10 0 1 -1 0 0 0 1 -1 
2

短,更快的是:

> set.seed(100) 
> df = data.frame(id = 1:10, replicate(8,sample(0:1,10,rep=TRUE))) 
> df 
    id X1 X2 X3 X4 X5 X6 X7 X8 
1 1 0 1 1 0 0 0 0 0 
2 2 0 1 1 1 1 0 1 0 
3 3 1 0 1 0 1 0 1 1 
4 4 0 0 1 1 1 0 1 1 
5 5 0 1 0 1 1 1 0 1 
6 6 0 1 0 1 0 0 0 1 
7 7 1 0 1 0 1 0 0 1 
8 8 0 0 1 1 1 0 0 1 
9 9 1 0 1 1 0 1 0 1 
10 10 0 1 0 0 0 0 1 0 

> cbind(df[,1:2],df[,3:ncol(df)] - df[,2:(ncol(df) - 1)]) 
    id X1 X2 X3 X4 X5 X6 X7 X8 
1 1 0 1 0 -1 0 0 0 0 
2 2 0 1 0 0 0 -1 1 -1 
3 3 1 -1 1 -1 1 -1 1 0 
4 4 0 0 1 0 0 -1 1 0 
5 5 0 1 -1 1 0 0 -1 1 
6 6 0 1 -1 1 -1 0 0 1 
7 7 1 -1 1 -1 1 -1 0 1 
8 8 0 0 1 0 0 -1 0 1 
9 9 1 -1 1 0 -1 1 -1 1 
10 10 0 1 -1 0 0 0 1 -1 
相關問題