R 2.13.1我試圖導入一個數據文件,其中包含千位分隔符和逗號作爲小數點的點,以及尾數減去負值。在Mac OS X上導入包含逗號,千分隔符和尾部減號的CSV數據
基本上,我嘗試轉換從:
"A|324,80|1.324,80|35,80-"
到
V1 V2 V3 V4
1 A 324.80 1324.8 -35.80
現在,交互以下工作都:
gsub("\\.","","1.324,80")
[1] "1324,80"
gsub("(.+)-$","-\\1", "35,80-")
[1] "-35,80"
,並結合他們:
gsub("\\.", "", gsub("(.+)-$","-\\1","1.324,80-"))
[1] "-1324,80"
不過,我無法從read.data除去千個分隔符:
setClass("num.with.commas")
setAs("character", "num.with.commas", function(from) as.numeric(gsub("\\.", "", sub("(.+)-$","-\\1",from))))
mydata <- "A|324,80|1.324,80|35,80-"
mytable <- read.table(textConnection(mydata), header=FALSE, quote="", comment.char="", sep="|", dec=",", skip=0, fill=FALSE,strip.white=TRUE, colClasses=c("character","num.with.commas", "num.with.commas", "num.with.commas"))
Warning messages:
1: In asMethod(object) : NAs introduced by coercion
2: In asMethod(object) : NAs introduced by coercion
3: In asMethod(object) : NAs introduced by coercion
mytable
V1 V2 V3 V4
1 A NA NA NA
需要注意的是,如果我改變從「\\」。到「」在功能上,事情看起來有點不同:
setAs("character", "num.with.commas", function(from) as.numeric(gsub(",", "", sub("(.+)-$","-\\1",from))))
mytable <- read.table(textConnection(mydata), header=FALSE, quote="", comment.char="", sep="|", dec=",", skip=0, fill=FALSE,strip.white=TRUE, colClasses=c("character","num.with.commas", "num.with.commas", "num.with.commas"))
mytable
V1 V2 V3 V4
1 A 32480 1.3248 -3580
我認爲這個問題是與DEC =該read.data「」將輸入的「」地「」在調用之前(來自「num.with.commas」),以便輸入字符串可以是例如「1.324.80」。
我希望(「1.123,80 - 」,「num.with.commas」)返回-1123.80並返回1100123.80(「1.100.123,80」,「num.with.commas」)。
如何讓我的num.with.commas替代輸入字符串中最後一個小數點以外的所有?
更新:首先,我添加了否定先行並在控制檯的工作得到了爲():
setAs("character", "num.with.commas", function(from) as.numeric(gsub("(?!\\.\\d\\d$)\\.", "", gsub("(.+)-$","-\\1",from), perl=TRUE)))
as("1.210.123.80-","num.with.commas")
[1] -1210124
as("10.123.80-","num.with.commas")
[1] -10123.8
as("10.123.80","num.with.commas")
[1] 10123.8
然而,仍然函數read.table有同樣的問題。添加一些print()到我的函數顯示num.with.commas實際上得到了逗號而不是點。
所以我現在的解決方案是從「,」替換爲「。」。在num.with.commas。
setAs("character", "num.with.commas", function(from) as.numeric(gsub(",","\\.",gsub("(?!\\.\\d\\d$)\\.", "", gsub("(.+)-$","-\\1",from), perl=TRUE))))
mytable <- read.table(textConnection(mydata), header=FALSE, quote="", comment.char="", sep="|", dec=",", skip=0, fill=FALSE,strip.white=TRUE, colClasses=c("character","num.with.commas", "num.with.commas", "num.with.commas"))
mytable
V1 V2 V3 V4
1 A 324.8 1101325 -35.8
是的,這工作得更好的解決方案 - 謝謝! –