R程序中的剪切功能 [英] Cut Function in R program
问题描述
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
所以我有关于时间和速度的数据...我想使用cut或which函数将数据分成6分钟的间隔...我的最大时间通常3000 mins
所以我希望输出类似于此...
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
所以我到目前为止,我是使用data = read.delim( clipboard)
读取数据的。我决定使用函数哪个...。但是我需要做3000分钟等等。
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
但是,如果我有时间上去3000分钟
,这将不是那么方便。结果包含在一个输出/变量中
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
谢谢大家
推荐答案
我将在这里假设您确实不想在各个容器中重复这些值。
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
这篇关于R程序中的剪切功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!