0

So I have data regarding Id number and time

Id number   Time(hr)
1   5
2   6.1
3   7.2
4   8.3
5   9.6
6   10.9
7   13
8   15.1
9   17.2
10  19.3
11  21.4
12  23.5
13  25.6
14  27.1
15  28.6
16  30.1
17  31.8
18  33.5
19  35.2
20  36.9
21  38.6
22  40.3
23  42
24  43.7
25  45.4

I want this output

   Time Id number
    10  5
    20  10
    30  16
    40  22

So I want the time to be in 10 hour intervals and get the ID that corresponds to that particular hour...I decided to use this code data <- data2[seq(0, nrow(data2), by=5), ] but instead of the Time being in 10 hr intervals...the ID number is at 10 intervals....but I dont want that output..so far I'm getting this output

Id.number Time..s.
        10     19.3
      20     36.9
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
  • 1
    How do your "Time" values magically become multiples of 10 in your output? – A5C1D2H2I1M1N2O1R2T1 Mar 11 '13 at 10:35
  • No the first output is just something I did in excel but for R I'm getiing the output above – Marco De Niro Mar 11 '13 at 11:31
  • 1
    Please give sample data or reproducible example so that good people here can help you better. See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – CHP Mar 11 '13 at 12:12

2 Answers2

0

You can use %% (mod) operator.

data[data$Time %% 10 == 0, ]

CHP
  • 16,981
  • 4
  • 38
  • 57
  • I got this output: <0 rows> (or 0-length row.names)...sadly – Marco De Niro Mar 11 '13 at 11:33
  • How do you get Time as multiple of 10 is different question. You can't get a value of Time = 10 , if there is no row in your data set with Time = 10. – CHP Mar 11 '13 at 11:35
0

I use cut() and cumsum(table()) but I don't quite get the answer you are expecting. How exactly are you calculating this?

# first load the data
v.txt <- '1   5
2   6.1
3   7.2
4   8.3
5   9.6
6   10.9
7   13
8   15.1
9   17.2
10  19.3
11  21.4
12  23.5
13  25.6
14  27.1
15  28.6
16  30.1
17  31.8
18  33.5
19  35.2
20  36.9
21  38.6
22  40.3
23  42
24  43.7
25  45.4'
# load in the data... awkwardly...
v <- as.data.frame(matrix(as.numeric(unlist(strsplit(strsplit(v.txt, '\n')[[1]], ' +'))), byrow=T, ncol=2))
tens <- seq(from=0, by=10, to=100)
v$cut <- cut(v$Time, tens, labels=tens[-1])
v2 <- as.data.frame(cumsum(table(v$cut)))
names(v2) <- 'Time'
v2$Id <- rownames(v2)
rownames(v2) <- 1:nrow(v2)
v2 <- v2[,c(2,1)]
rm(v, v.txt, tens) # not needed anymore
v2 # the answer... but doesn't quite match your expected answer...

  Id Time
1 10    5
2 20   10
3 30   15
4 40   21
5 50   25
Sean
  • 3,765
  • 3
  • 26
  • 48
  • So what I want is time to be divided into 10 hr intervals...and during the 10th,20th hour etc which ID number does it correspond to – Marco De Niro Mar 11 '13 at 11:36
  • How much data do you have, and how many 10hr intervals are there? Can you have a tie? i.e. Time values of c(40.1, 40.1, 40.2) ? which one do you take? – Sean Mar 11 '13 at 12:07