0

I have a column in my data that has just the minute corresponding to the time of an error code. I want to create a new column called i$rminute that splits the time into four, fifteen minute intervals.

>for(j in i$rminute) {
  if(i$rminute < 16){
    i$r15intervals <- "1"
  } else if(i$rminute < 31){
    i$r15intervals <- "2"
  } else if(i$rminute < 46){
    i$r15intervals <- "3"
  } else {
    i$r15intervals <- "4"}}


>i$r15intervals <- as.factor(i$r15intervals)


   > summary(i$r15intervals)
    2 
78455 

So the only number it returns is 2, What am I doing wrong? why is it not creating a new column with with variables 1 through 4 that corresponds to each fifteen minute interval?

I'm still new to R, i'm guessing there is something very obvious that I'm doing wrong.

Coopa
  • 213
  • 1
  • 12
  • Since you are looping using a for cycle, you are overwriting your i$r15intervals column with one single number at each cycle – Damiano Fantini Aug 29 '17 at 17:26
  • first I would recommend not calling your data frame `i` particularly within a loop as it is rather confusing, second please provide an example of your data (`dput` is great for this), third there is no reason to be using a for loop for this... – B Williams Aug 29 '17 at 17:27
  • 1
    This is very un-R-ish. You should use `findInterval()` or `cut()` – M-- Aug 29 '17 at 17:27
  • 2
    Try using the cut command: `cut(i$minute, breaks=c(0, 16, 31, 46, Inf))` it will be much much faster than a loop. – Dave2e Aug 29 '17 at 17:28

3 Answers3

1

You're assigning the whole column value with the i$r15intervals <- #, so probably your last number in i$rminute is between 16 and 31.

Set just the row value for each by doing i[j, "intervals"].

Here's another way to do it without the for loop

i = data.frame(rminute = sample(1:50, 20))


findInterval = function(x){
  if(x< 16){
      return("1")
  } else if(x < 31){
      return("2")
  } else if(x< 46){
      return("3")
  } else {
    return("4")}
}

i$r15interval = sapply(i$rminute, findInterval)

> head(i)
  rminute r15interval
1       1           1
2      47           4
3      25           2
4      41           3
5      16           2
6      44           3
Kristofersen
  • 2,736
  • 1
  • 15
  • 31
1

This will make the factor that you're looking for:

cut(i$rminute, c(0, 16, 31, 46, Inf), 1:4)
Jake Kaupp
  • 7,892
  • 2
  • 26
  • 36
0

Use the cut() function :

cut(c(20,36,8,47),breaks = c(0,15,30,45,60))

And the output is :

[1] (15,30] (30,45] (0,15]  (45,60] 
Levels: (0,15] (15,30] (30,45](45,60]
H.Sechier
  • 281
  • 3
  • 7