繁体   English   中英

将包含“。”的因子转换为数值

[英]Convert factor that includes “.” to numeric

我使用的数据集使用句点( . )代替NA 现在,我期待在列是水平的因素12 ,和. 我试图取个平均数,显然na.rm无法正常工作。 我回过头来,通过将周期更改为pe94[pe94 == "."] <- NApe94[pe94 == "."] <- NA来清理数据,这似乎可行。 但是, mean不能取一个因子的平均值,当我将该因子转换为数字时, NA变为3 s。 我如何摆脱这个问题?

我也遇到过类似的问题(以及其他问题),将因子转换为数字以进行数学分析。 但是,我发现了一个似乎很简单的解决方案。 希望这可以帮助 ...

#Script to convert factor data to numeric data without loss or alterations of values

#Samlpe data frame with factor variables represented by numbers 
factor.vector1<-factor(x=c(111,222,333,444,555))
thousands<-c("1,000","2,000","3,000","4,000","5,000")
factor.vector2<-factor(x=thousands)
df<-data.frame(factor.vector1, factor.vector2)

#Numbers as factors without comma place holders
#1st convert dataset to character data type
df[,1]<-as.character(df[,1])
#2nd convert dataset to numeric data type
df[,1]<-as.numeric(df[,1])

#Numbers as factors WITH comma place holders 
#If data contains commas in the numbers (e.g. 2,000) use gsub to remove commas
#If commas are not removed before conversion, the value containing commas will become NA
df[,2]<-gsub(",", "", df[,2])
#1st convert dataset to character data type
df[,2]<-as.character(df[,2])
#2nd convert dataset to numeric data type
df[,2]<-as.numeric(df[,2])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM