0

Some example code first:

Scores = c(5,4,3,2,1)
Breaks = c(0,0.6,0.8,1,1.2,100)
cut(1.19,breaks=Breaks, labels=Scores)
as.numeric(cut(1.19,breaks=Breaks,labels=Scores)

The first Output ist correctly 2, the second is wrong with 4. Why is there this difference? I actually need a numeric value, but the correct one...so how do I convert it correctly?

MichiZH
  • 5,587
  • 12
  • 41
  • 81

2 Answers2

4

cut returns a factor and 2 is the 4th factor level. You need to convert factors to character first if you want the level labels as numerics:

as.numeric(as.character(cut(1.19,breaks=Breaks,labels=Scores)))
[1] 2
James
  • 65,548
  • 14
  • 155
  • 193
1

You need to get the levels of the factor out for each element. Try:

f <- cut(1.19,breaks=Breaks,labels=Scores)
as.numeric( levels(f)[f] )

(While James' answer works fine too this might help you a bit with understanding the factor)

John
  • 23,360
  • 7
  • 57
  • 83