as.numeric函数更改我的数据框中的值 [英] The as.numeric function changes the values in my dataframe

查看:920
本文介绍了as.numeric函数更改我的数据框中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一列包含速度测量值,需要将其更改为数字,以便可以使用均值和求和函数.但是,当我将其转换时,值会发生很大变化.

I have a column containing speed measurements which I need to change to numeric so that I can use both the mean and sum functions. However, when I do convert them the values change substantially.

这是为什么?

这是我最初的数据:

这是数据帧的结构:

'data.frame':   1899571 obs. of  20 variables:
 $ pcd        : Factor w/ 1736958 levels "AB101AA","AB101AB",..: 1 2 3 4 5 6 6 7 7 8 
 $ pcdstatus  : Factor w/ 5 levels "Insufficient Data",..: 4 4 4 4 4 2 3 2 3 3 ...
 $ mbps2      : Factor w/ 3 levels "N","N/A","Y": 2 2 2 2 2 2 2 2 2 2 ...
 $ averagesp  : Factor w/ 301 levels ">=30","0","0.2",..: 301 301 301 301 301 301 301 
 $ mediansp   : Factor w/ 302 levels ">=30","0","0.1",..: 302 302 302 302 302 302 302 
 $ maxsp      : Factor w/ 301 levels ">=30","0","0.2",..: 301 301 301 301 301 301 301 
 $ nga        : Factor w/ 2 levels "N","Y": 1 2 1 1 1 1 1 2 2 2 ...
 $ connections: Factor w/ 119 levels "<3","0","1","10",..: 2 2 2 2 2 1 2 1 2 2 ...
 $ pcd2       : Factor w/ 1736958 levels "AB10 1AA","AB10 1AB",..: 1 2 3 4 5 6 6 7 7 8 
 $ pcds       : Factor w/ 1736958 levels "AB10 1AA","AB10 1AB",..: 1 2 3 4 5 6 6 7 7 8 
 $ oslaua     : Factor w/ 407 levels "","95A","95B",..: 374 374 374 374 374 374 374 
 $ x          : int  394251 394232 394181 394251 394371 394181 394181 394331 394331 
 $ y          : int  806376 806470 806429 806376 806359 806429 806429 806530 806530 
 $ ctry       : Factor w/ 4 levels "E92000001","N92000002",..: 3 3 3 3 3 3 3 3 3 3 ...
 $ hro2       : Factor w/ 13 levels "","E12000001",..: 12 12 12 12 12 12 12 12 12 12 
 $ soa1       : Factor w/ 34381 levels "","E01000001",..: 32485 32485 32485 32485 
 $ dzone1     : Factor w/ 6507 levels "","E99999999",..: 128 128 128 128 112 128 128 
 $ soa2       : Factor w/ 7197 levels "","E02000001",..: 6784 6784 6784 6784 6784 6784 
 $ urindew    : int  9 9 9 9 9 9 9 9 9 9 ...
 $ soa1ni     : Factor w/ 892 levels "","95AA01S1",..: 892 892 892 892 892 892 892 892 

这是将我的变量转换为数字变量的代码.

This is the code for converting my variables to numeric variables.

 #convert individual columns to numeric variables  
 total$averagesp <- as.numeric(total$averagesp) 
 total$mediansp <- as.numeric(total$mediansp) 
 total$maxsp <- as.numeric(total$maxsp) 
 total$mbps2 <- as.numeric(total$mbps2)
 total$nga <- as.numeric(total$nga)
 total$connections <- as.numeric(total$connections)

但是后来我得到了一个奇怪的输出,我的所有数据都被夸大了:

But I have this strange output afterwards where all my data have been inflated:

任何帮助将不胜感激-谢谢!

Any help would be much appreciated - thank you!

推荐答案

请参见

See FAQ 7.10. Basically when you use as.numeric on a factor then you get the underlying integers. The FAQ has the recipes for turning them into the numbers represented by the strings.

这篇关于as.numeric函数更改我的数据框中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆