psysky
psysky

Reputation: 3195

incorrect convert chr to numeric variable in R

I have these data

   X    json_data.time.updated json_data.time.updatedISO  json_data.time.updateduk code       rate          description
1  1 Jan 19, 2019 15:18:00 UTC 2019-01-19T15:18:00+00:00 Jan 19, 2019 at 15:18 GMT  USD 3,735.7750 United States Dollar
2  1 Jan 19, 2019 15:19:00 UTC 2019-01-19T15:19:00+00:00 Jan 19, 2019 at 15:19 GMT  USD 3,735.9150 United States Dollar
3  1 Jan 19, 2019 15:51:00 UTC 2019-01-19T15:51:00+00:00 Jan 19, 2019 at 15:51 GMT  USD 3,736.9100 United States Dollar
4  1 Jan 19, 2019 15:52:00 UTC 2019-01-19T15:52:00+00:00 Jan 19, 2019 at 15:52 GMT  USD 3,735.3200 United States Dollar
5  1 Jan 19, 2019 15:54:00 UTC 2019-01-19T15:54:00+00:00 Jan 19, 2019 at 15:54 GMT  USD 3,736.7717 United States Dollar
6  1 Jan 19, 2019 15:55:00 UTC 2019-01-19T15:55:00+00:00 Jan 19, 2019 at 15:55 GMT  USD 3,736.0750 United States Dollar
7  1 Jan 19, 2019 15:57:00 UTC 2019-01-19T15:57:00+00:00 Jan 19, 2019 at 15:57 GMT  USD 3,734.9600 United States Dollar
8  1 Jan 19, 2019 15:58:00 UTC 2019-01-19T15:58:00+00:00 Jan 19, 2019 at 15:58 GMT  USD 3,734.9117 United States Dollar
9  1 Jan 19, 2019 16:00:00 UTC 2019-01-19T16:00:00+00:00 Jan 19, 2019 at 16:00 GMT  USD 3,734.2833 United States Dollar
10 1 Jan 19, 2019 16:01:00 UTC 2019-01-19T16:01:00+00:00 Jan 19, 2019 at 16:01 GMT  USD 3,734.4950 United States Dollar
   rate_float
1    3735.775
2    3735.915
3     3736.91
4     3735.32
5   3736.7717
6    3736.075
7     3734.96
8   3734.9117
9   3734.2833
10   3734.495

I need that df$rate_float was numeric variable

I do so

df$rate_float<-as.numeric(df$rate_float)

but as result, df$rate_float just becomes by order

1
2
3
4

lost real values. How convert chr to num, that original values didn't disappear?

Upvotes: 1

Views: 71

Answers (1)

Julius Vainora
Julius Vainora

Reputation: 48241

You haven't included dput of your data, but apparently rate_float is a factor rather than a character. In that case you need

as.numeric(as.character(df$rate_float))

Meanwhile, you approach gives something like factor IDs.

Upvotes: 1

Related Questions