2013-03-26 65 views
0
Time Velocity 
0 0 
1.5 1.21 
3 1.26 
4.5 1.31 
6 1.36 
7.5 1.41 
9 1.46 
10.5 1.51 
12 1.56 
13 1.61 
14 1.66 
15 1.71 
16 1.76 
17 1.81 
18 1.86 
19 1.91 
20 1.96 
21 2.01 
22.5 2.06 
24 2.11 
25.5 2.16 
27 2.21 
28.5 2.26 
30 2.31 
31.5 2.36 
33 2.41 
34.5 2.4223 
36 2.4323 

所以我有關於時間和速度的數據......我想使用剪切或哪個函數函數將數據分隔成6分鐘的間隔......我的最大時間通常會增加到3000分鐘 所以我想在輸出中,以與此類似...R程序中的剪切功能

Time Velocity 
0 0 
1.5 1.21 
3 1.26 
4.5 1.31 
6 1.36 
Time Velocity 
6 1.36 
7.5 1.41 
9 1.46 
10.5 1.51 
12 1.56 

Time Velocity 
12 1.56 
13 1.61 
14 1.66 
15 1.71 
16 1.76 
17 1.81 
18 1.86 

因此,我所做到目前爲止讀取數據使用= read.delim(「剪貼板」) 我決定將數據使用功能'哪個'...但我需要做的最多3000分鐘等

dat <- data[which(data$Time>=0 
& data$Time < 6),], 
dat1 <- data[which(data$Time>=6 
& data$Time < 12),] 

等 但是,這不會是太方便了,如果我有時間走到3000分鐘 我也希望我的所有結果將被包含在一個輸出/可變

謝謝你們

+1

您確定您希望time = 6,12,18 ...的值在兩個時間間隔內重複嗎? – 2013-03-26 02:32:30

+0

要將數據分成這些組,要使用的R函數是「cut」。如果你能解釋爲什麼你會選擇使用重複的邏輯切割,我全都是耳朵。另外,在這種情況下你不需要'which'。只要你聰明地處理丟失的數據,'data [data $ Time> = 6&data $ Time <12,]'將會工作。 – Justin 2013-03-26 02:39:51

+0

我想使用剪切,但我不知道如何......它是像dat <-cut(dat $時間,休息= C(0,1,30,600),標籤= F)......但仍然困惑...因爲我沒有得到正確的輸出 – 2013-03-26 02:43:55

回答

3

我將承擔在這裏,你真的不想重複跨過垃圾箱的值。

cuts = cut(data$Time, seq(0, max(data$Time)+6, by=6), right=FALSE) 
x <- by(data, cuts, FUN=I) 

x 
## cuts: [0,6) 
## Time Velocity 
## 1 0.0  0.00 
## 2 1.5  1.21 
## 3 3.0  1.26 
## 4 4.5  1.31 
## ------------------------------------------------------------------------------------------------------------ 
## cuts: [6,12) 
## Time Velocity 
## 5 6.0  1.36 
## 6 7.5  1.41 
## 7 9.0  1.46 
## 8 10.5  1.51 
## ------------------------------------------------------------------------------------------------------------ 
## <snip> 
## ------------------------------------------------------------------------------------------------------------ 
## cuts: [36,42) 
## Time Velocity 
## 28 36 2.4323 
+0

感謝馬修......我想知道你是否可以幫我做一個t.test .. – 2013-03-26 04:51:41

0

如果你真的需要重複時間戳是6個積分multipes那麼你將不得不拆分之前做一些數據複製。

txt <- "Time Velocity\n0 0\n1.5 1.21\n3 1.26\n4.5 1.31\n6 1.36\n7.5 1.41\n9 1.46\n10.5 1.51\n12 1.56\n13 1.61\n14 1.66\n15 1.71\n16 1.76\n17 1.81\n18 1.86\n19 1.91\n20 1.96\n21 2.01\n22.5 2.06\n24 2.11\n25.5 2.16\n27 2.21\n28.5 2.26\n30 2.31\n31.5 2.36\n33 2.41\n34.5 2.4223\n36 2.4323" 

DF <- read.table(text = txt, header = TRUE) 

# Create duplicate timestamps where timestamp is multiple of 6 second 
posinc <- DF[DF$Time%%6 == 0, ] 
neginc <- DF[DF$Time%%6 == 0, ] 
posinc <- posinc[-1, ] 
neginc <- neginc[-1, ] 

# Add tiny +ve and -ve increments to these duplicated timestamps 
posinc$Time <- posinc$Time + 0.01 
neginc$Time <- neginc$Time - 0.01 

# Bind original dataframe without 6 sec multiple timestamp with above duplicated timestamps 
DF2 <- do.call(rbind, list(DF[!DF$Time%%6 == 0, ], posinc, neginc)) 

# Order by timestamp 
DF2 <- DF2[order(DF2$Time), ] 

# Split the dataframe by quotient of timestamp divided by 6 
SL <- split(DF2, DF2$Time%/%6) 

# Round back up the timestamps of split data to 1 decimal place 
RESULT <- lapply(SL, function(x) { 
    x$Time <- round(x$Time, 1) 
    return(x) 
}) 

RESULT 
## $`0` 
## Time Velocity 
## 2 1.5  1.21 
## 3 3.0  1.26 
## 4 4.5  1.31 
## 51 6.0  1.36 
## 
## $`1` 
## Time Velocity 
## 5 6.0  1.36 
## 6 7.5  1.41 
## 7 9.0  1.46 
## 8 10.5  1.51 
## 91 12.0  1.56 
## 
## $`2` 
##  Time Velocity 
## 9  12  1.56 
## 10 13  1.61 
## 11 14  1.66 
## 12 15  1.71 
## 13 16  1.76 
## 14 17  1.81 
## 151 18  1.86 
## 
## $`3` 
##  Time Velocity 
## 15 18.0  1.86 
## 16 19.0  1.91 
## 17 20.0  1.96 
## 18 21.0  2.01 
## 19 22.5  2.06 
## 201 24.0  2.11 
## 
## $`4` 
##  Time Velocity 
## 20 24.0  2.11 
## 21 25.5  2.16 
## 22 27.0  2.21 
## 23 28.5  2.26 
## 241 30.0  2.31 
## 
## $`5` 
##  Time Velocity 
## 24 30.0 2.3100 
## 25 31.5 2.3600 
## 26 33.0 2.4100 
## 27 34.5 2.4223 
## 281 36.0 2.4323 
## 
## $`6` 
## Time Velocity 
## 28 36 2.4323 
## 
2

我不認爲你想得到重複的界限。這裏一個簡單的解決方案,不用cut(類似於@Mathew解決方案)。

dat <- transform(dat, index = dat$Time %/% 6) 
    by(dat,dat$index,FUN=I)