Yes. Two methods. The easiest way to understand at first is probably just to as.is=TRUE as character vectors, and then use gsub to remove commas and any currency characters before converting to numeric. The second is a bit more complicated, but I think more. Create an as-method for the format you are using. Then you can use colClasses to do this in one step.
I see that @EDi has already executed version # 1 (using stringsAsFactors , not as.is , so I will document Strategy # 2:
library(methods) setClass("num.with.commas") #[1] "num.with.commas" setAs("character", "num.with.commas", function(from) as.numeric(gsub(",", "", from))) require(RCurl) #Loading required package: RCurl #Loading required package: bitops myCsv <- getURL("https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0Agbdciapt4QZdE95UDFoNHlyNnl6aGlqbGF0cDIzTlE&single=true&gid=0&range=A1%3AG4928&output=csv", ssl.verifypeer=FALSE) > fullmatrix <- read.csv(textConnection(myCsv), colClasses=c(rep("num.with.commas",2), rep("numeric",4) )) str(fullmatrix) #-------------- 'data.frame': 4927 obs. of 7 variables: $ wave. : num 9999 9997 9995 9993 9992 ... $ wavelength : num 1000 1000 1000 1001 1001 ... $ d2o : num 85.2 87.7 86.3 87.6 85.6 ... $ di : num 54.3 55.8 54.9 55.6 54.9 ... $ ddw : num 48.2 49.7 49.4 50.2 49.6 ... $ ddw.old : num 53.3 55 53.9 54.8 53.7 ... $ d2o.ddw.mix: num 65.8 67.9 67.2 68.4 66.8 ...
as methods are mandatory. There are many methods in the R database, such as as.list , as.numeric , as.character . In each case, they try to enter input that is in one mode and make a reasonable copy of it in another mode. For example, it makes sense to force the matrix to a data frame, because they both have two dimensions. This is slightly less meaningful to force a data block into a matrix (but it succeeds with losing all the attributes of the columns and enforcing general mode.)
In this case, I take the character string as input, removing any commas and changing the character values โโto numeric. Then I use the read.table (in this case, using read.csv ) the colClasses argument to send to the as method I registered with setAs . For more details, you can go to the help(setAs) page. The S4 class system confuses many people, including me. This is the only area of โโsuccess I have had with S4 methods.
source share