R: How to read in numbers with comma as a Dec separator & a Field separator? “The two arguments to fread 'dec' and 'sep' are equal (',').”

北城以北 提交于 2019-12-13 23:43:00

问题


This is partially related to reading in files in so-called European way, more in How to read in numbers with a comma as decimal separator?. I have data with a row such as "Invoice","1324","Name","John","Age","10","Height","143,5","Products","1;2;3;4","ProductIDs","01;02;03;04" where a comma acts as a separator of field values and inside the field values, delimited with double-quotes, comma acts as a decimal separator.

Inside field values, the semicolon also acts as other separator but we can exclude this observation for now on and concentrate on correctly first reading in the file with commas having different meaning in different places.

How to read in numbers with a comma as a decimal separator and a field separator in R?


回答1:


It might be possible to do using the dec parameter depending on how you're reading the file in. Here is how I would do it using data.table:

dat <- fread('"Name", "Age"
              "Joe", "1,2"')
dat[, Age := as.numeric(gsub(",", ".", Age))]

#    Name Age
# 1:  Joe 1.2



回答2:


How about this?

read.table("file.name", sep=",", quote = "\"", dec=",")


来源:https://stackoverflow.com/questions/44855415/r-how-to-read-in-numbers-with-comma-as-a-dec-separator-a-field-separator-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!