1

I have a dataset that kind a looks like this:

head(data)
spotify_id                   title_song               code   genre  subgenre        artiest acousticness  valence   energy danceability instrumentallness  speechiness id
 1 spotify:track:30ZGbfPsjDNCgL21Qz                   Summertime  TRBIMVX144D110CA63 country tennessee Kenny Chesney     0.213743 0.705857  0.934522     0.479442    0.000000111572   0.0995011  1

I need to subtract one the columns so there I want to make sure the values are numberic. Right now the column I need is a vector:

 class(data$acousticness)
 [1] "factor"

Thing is that when I do this:

 data$acousticness <- as.integer(data$acousticness)
 class(data$acousticness)
 [1] "integer"

I get the following values:

  head(data$energy) 
  [1] 5188 3674 4653 4272 3883 1774

Thats way different then the values before (5188 in stead of 0.21...). Any thoughts on what goes wrong here?

  • 2
    Try `as.numeric(as.character(data$acousticness))` instead. – A5C1D2H2I1M1N2O1R2T1 Nov 06 '15 at 15:13
  • 1
    R FAQ: [How do I convert factors to numeric?](https://cran.r-project.org/doc/FAQ/R-FAQ.html#How-do-I-convert-factors-to-numeric_003f) Without the `as.character` you instead get the number that R associates with the level of the factor. – WhiteViking Nov 06 '15 at 15:17

0 Answers0