转换文本时出错" 0"到数字0

时间:2016-04-03 20:51:29

标签: r csv

我有一个非常大的键值数据集,我从这样的CSV文件中导入并重新整形为一个很长的表:

#read in suitability data file
allapp <- read.csv(file="Dist.csv", header=TRUE, sep=",")

#clip and reshape key values into a table
appdata <- subset(allapp, grepl("^SUIT*", allapp$XmlKey))
appdata <- select(appdata, TransIdentifier, XmlKey, XmlValue)
appdata <-spread(appdata, XmlKey, XmlValue)

所有字段都以文本值的形式输入。当我将列转换为数值时,所有非零字段都可以正确转换:

#Income & expense analysis - source
appdata <- rename(appdata, Monthly_Income = SUIT42)
appdata$Monthly_Income <- as.numeric(appdata$Monthly_Income)

然而,出于某种原因,每个&#34; 0&#34;被转换为数字71。 当我将表写入csv文件时,&#34; 0&#34; s看起来很正常:

  "0","100000","0","0","0","0","0","0","0","0",NA,"0","0","0","0","0","0","0","0",NA,"0","0","false",NA,

知道为什么吗?

1 个答案:

答案 0 :(得分:1)

使用:

解决
appdata$Monthly_Income <- as.numeric (as.character(appdata$Monthly_Income))