问题
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