0

I want to assign a score to a value based on which bucket it fits in. The number line is split into four buckets: (-Inf, 0], (0, 100], (100, 1000], (1000, Inf). The buckets are scored 1 through 4.

Here's the code I wrote using if/else, but it seems like R would have something to do this kind of task.

if (min.coverage > 1000) {
    on.score <- 4
}
else if (min.coverage > 100) {
    on.score <- 3
}
else if (min.coverage > 0) {
    on.score <- 2
}
else {
    on.score <- 1
}

Update

Thanks to MrFlick for pointing me to the question on how to cut by defined intervals. It covers most of what I asked, but not how to assign a different score to each interval.

Community
  • 1
  • 1
Don Kirkby
  • 53,582
  • 27
  • 205
  • 286

1 Answers1

0

The cut() function did what I wanted, but it took a few tries to persuade it. Here's my working code that assigns the score as a string. (Good enough for me.) If I didn't do the as.character() conversion, I got the bucket index instead of the score when I tried to write it to an output file.

on.score <- as.character(cut(
        min.coverage,
        c(-Inf, 0, 100, 1000, Inf),
        labels=c(1, 2, 3, 4)))
Don Kirkby
  • 53,582
  • 27
  • 205
  • 286