Ian
Ian

Reputation: 31

Any easy way to subset my consecutive data?

One of my data attributes is "Time",from o seconds to 8640 seconds (consecutive), my professor ask me to analysis my data (0-5 mins) (5-10 mins) (10 -15 mins) separately. I know the basic code to subset my data, like:

data<-data[which(data$Time<=300&data$>=0),]

But if I do in this way, I have to repeat again again and again... I am thinking, can I have an easy way to solve this problem? Anyone can help me? Thank you very much!!

Upvotes: 0

Views: 129

Answers (1)

gented
gented

Reputation: 1697

Let us assume that your data set is as following, without loss of generality:

time <- seq(0,8640) 

define your unit blocks of time as (you are looking at units of 5 minutes)

blocks <- seq(1, length(time), 300)

and then

groups <- cbind(time, cut(time, blocks))

accompanies each row in time with the corresponding block of 5 minutes (variable V2) it corresponds to. In fact:

R: set.seed(1234)
R: groups[sample(nrow(groups), 5), ]
     time V2
983   983  4
5377 5377 18
5263 5263 18
5385 5385 18
7435 7435 25

Once so, you can perform any sort of operation of your data set grouping by the variable V2.

Upvotes: 1

Related Questions