as.numeric函数更改数据框中的值

as.numeric函数更改数据框中的值,r,sum,numeric,mean,R,Sum,Numeric,Mean,我有一列包含速度测量值,我需要将其更改为数值,这样我就可以使用均值和和和函数。但是,当我转换它们时,值会发生很大的变化 为什么会这样 这就是我的数据最初的样子: 以下是数据帧的结构: 'data.frame': 1899571 obs. of 20 variables: $ pcd : Factor w/ 1736958 levels "AB101AA","AB101AB",..: 1 2 3 4 5 6 6 7 7 8 $ p

我有一列包含速度测量值,我需要将其更改为数值,这样我就可以使用均值和和和函数。但是,当我转换它们时,值会发生很大的变化

为什么会这样

这就是我的数据最初的样子:

以下是数据帧的结构:

'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 
“数据帧”:1899571 obs。在20个变量中:
$pcd:系数w/1736958水平“AB101AA”,“AB101AB”,“1 2 3 4 5 6 7 8
$pcdstatus:系数w/5级别“数据不足”…:4 3 3。。。
$mbps2:系数w/3级“N”、“N/A”、“Y”:2。。。
$averagesp:系数w/301等级“>=30”、“0”、“0.2”、…:301
$mediansp:系数w/302级别“>=30”、“0”、“0.1”、..:302 302 302
$maxsp:系数w/301级“>=30”、“0”、“0.2”、..:301
$nga:系数w/2级“N”、“Y”:12。。。

$connections:Factor w/119 levels“请参见。基本上,当您在一个因子上使用
作为.numeric
时,您将得到基础整数。常见问题解答提供了将它们转换为字符串表示的数字的方法。

您希望R如何转换
“>=30”
”是的,但我并不是为了简洁而把所有的代码都放在这个问题上。在实际脚本中,我将所有这些字符转换为纯数字。然而,它仍然会膨胀我所有的数据?它不会“膨胀”。它使用的是因子值,而不是级别。在删除符号,然后以不同的顺序重新运行代码后,我做的最后一件事是将字符变量转换为数字,我解决了这个问题。谢谢,EdDo不要编辑标题以表示“已解决”。在正常情况下,一个被接受的答案可以达到这一目的。在这种情况下,您下面的答案不会真正帮助任何人,但指向重复项的指针会帮助他们,因为这才是问题的真正根源。谢谢
as.numeric(as.character(f))
对我很有用。我不得不将
添加为.character
 #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)