2013-03-26 60 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)