2016-04-03 24 views
0

我有一個非常大的鍵值數據集,我從一個CSV導入文件中像這樣和重塑成一個非常長的表:錯誤將文本轉換爲「0」,以數字0

#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) 

所有字段來作爲文本值。當我轉換一列數值,所有非零字段轉換正確:

​​

然而,出於某種原因,每一個「0」被轉換成71 當我寫的表到CSV數文件中,「0」的外觀正常:

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

任何想法爲什麼?

+2

嘗試:應用程序數據$ Monthly_Income < - as.numeric(as.character(應用程序數據$ Monthly_Income))。這些值很可能是作爲因素導入的,在這種情況下,您必須將其轉換爲字符,然後轉換爲數字值。 – Dave2e

+0

非常感謝。這解決了它。猜猜我需要更好地理解變量的轉換順序, –

+1

這是一個非常常見的錯誤,它已經燒燬了每個R用戶至少一次。以下是開始的一個很好的參考:http://www.cookbook-r.com/第6章操作數據。 – Dave2e

回答

1

通過解決:

appdata$Monthly_Income <- as.numeric (as.character(appdata$Monthly_Income)) 
相關問題