How to read data when some numbers contain commas as thousand separator?

后端 未结 11 1287
情书的邮戳
情书的邮戳 2020-11-22 02:29

I have a csv file where some of the numerical values are expressed as strings with commas as thousand separator, e.g. \"1,513\" instead of 1513. Wh

11条回答
  •  旧巷少年郎
    2020-11-22 03:00

    I want to use R rather than pre-processing the data as it makes it easier when the data are revised. Following Shane's suggestion of using gsub, I think this is about as neat as I can do:

    x <- read.csv("file.csv",header=TRUE,colClasses="character")
    col2cvt <- 15:41
    x[,col2cvt] <- lapply(x[,col2cvt],function(x){as.numeric(gsub(",", "", x))})
    

提交回复
热议问题