0

When I check the type, it said "integer", but has decimal point. If I change it to numeric, it become integer(no decimal point). Because I want to do histogram, x must be numeric, but if change to numeric, all data wrong.

> typeof(data$fare_amount)
[1] "integer"
> data$fare_amount
  [1] 5.5  6.5  8.0  13.5 5.5  9.5  7.5  8.0  16.0 8.0  5.5  7.0  8.0  5.0  9.5  23.0 5.0  6.0  17.5 12.0 8.5  13.0
 [23] 6.5  4.5  52.0 14.5 7.5  4.5  9.0  10.0 15.0 11.5 6.0  12.5 7.5  8.0  6.5  7.5  31.5 10.0 10.0 10.0 4.0  8.5 
 [45] 24.0 8.5  5.5  14.0 11.0 4.5  9.0  7.5  22.0 8.5  24.0 36.5 15.0 10.5 9.5  17.0 4.5  6.0  6.5  11.5 16.0 6.5 
 [67] 7.0  20.0 13.5 30.0 8.0  11.0 6.5  11.5 6.5  37.0 5.5  12.5 8.5  58.5 13.5 8.5  9.0  6.0  6.5  9.0  38.0 4.5 
 [89] 10.0 9.0  44.5 11.0 12.0 4.5  14.5 8.5  32.0 9.5  4.5  6.0  6.5  6.0  31.5 52.0 10.5 12.0 5.5  24.5 7.0  5.5 
[111] 16.5 5.0  5.5  6.5  3.5  11.5 13.0 6.0  14.0 3.5 
42 Levels: 13.5 16.0 5.5 6.5 7.5 8.0 9.5 12.0 17.5 23.0 5.0 6.0 7.0 10.0 13.0 14.5 4.5 52.0 8.5 9.0 11.5 12.5 ... 3.5
> temp <- as.numeric(data$fare_amount)
> temp
  [1]  3  4  6  1  3  7  5  6  2  6  3 13  6 11  7 10 11 12  9  8 19 15  4 17 18 16  5 17 20 14 23 21 12 22  5  6  4  5
 [39] 24 14 14 14 28 19 27 19  3 26 25 17 20  5 31 19 27 32 23 29  7 30 17 12  4 21  2  4 13 33  1 34  6 25  4 21  4 35
 [77]  3 22 19 36  1 19 20 12  4 20 37 17 14 20 39 25  8 17 16 19 38  7 17 12  4 12 24 18 29  8  3 40 13  3 41 11  3  4
[115] 42 21 15 12 26 42
user2722968
  • 13,636
  • 2
  • 46
  • 67
Skyline
  • 61
  • 2
  • 8
  • 4
    the type is integer, but your variable class is a factor. try `as.numeric(as.character(x))` first – Zelazny7 Nov 17 '18 at 14:37
  • @Zelazny7 Yes, it works using `as.numeric(as.character(data$fare_amount))`, thanks a lot. – Skyline Nov 17 '18 at 14:42
  • 4
    The bigger thing you should address is why that field is a factor in the first place. How are you reading it in? R thinks there's a character string in the data somewhere and treating it as a factor. – Zelazny7 Nov 17 '18 at 14:56
  • 4
    Check classic pitfalls: single comma decimal separator? decimal separator in your locale? NA coded as some other character? dollar signs? etc etc etc. – Henrik Nov 17 '18 at 15:01
  • http://notstatschat.tumblr.com/post/124987394001/stringsasfactors-sigh – Ben Bolker Nov 17 '18 at 15:14

0 Answers0