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

后端 未结 11 1435
情书的邮戳
情书的邮戳 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 02:57

    Not sure about how to have read.csv interpret it properly, but you can use gsub to replace "," with "", and then convert the string to numeric using as.numeric:

    y <- c("1,200","20,000","100","12,111")
    as.numeric(gsub(",", "", y))
    # [1]  1200 20000 100 12111
    

    This was also answered previously on R-Help (and in Q2 here).

    Alternatively, you can pre-process the file, for instance with sed in unix.

提交回复
热议问题