问题
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
So I have data about Time and Velocity...I want to use the cut or the which function to separate my data into 6 min intervals...my Maximum Time usually goes up to 3000 mins So I would want the output to be similar to this...
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
So what I did so far is read the data using data=read.delim("clipboard") I decided to use the function 'which'....but I would need to do it for up 3000 mins etc
dat <- data[which(data$Time>=0
& data$Time < 6),],
dat1 <- data[which(data$Time>=6
& data$Time < 12),]
etc But this wouldn't be so convenient if I had time to went up to 3000 mins Also I would want all my results to be contained in one output/ variable
Thanks guys
回答1:
I will assume here that you really don't want to duplicate the values across the bins.
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
回答2:
I don't think that you want to get duplicated bounds. here A simple solution without using cut
( similar to @Mathew solution).
dat <- transform(dat, index = dat$Time %/% 6)
by(dat,dat$index,FUN=I)
回答3:
If you really need duplicates timestamps that are integral multipes of 6 then you will have to do some data duplication before splitting.
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
##
来源:https://stackoverflow.com/questions/15628289/cut-function-in-r-program