R:如何使用逗号作为Dec分隔符&来读取数字字段分隔符? “读取'dec'和'sep'的两个参数相等(',')。 [英] 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 (',')."

查看:90
本文介绍了R:如何使用逗号作为Dec分隔符&来读取数字字段分隔符? “读取'dec'和'sep'的两个参数相等(',')。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这部分与以所谓的欧洲方式读取文件有关,更多信息在如何读取以逗号作为小数点分隔符的数字?。我有一行数据,例如发票, 1324,名称, John,年龄, 10,高度, 143,5,产品 , 1; 2; 3; 4, ProductIDs, 01; 02; 03; 04 ,其中逗号用作字段值的分隔符,并在字段值内以逗号分隔双引号,逗号用作小数点分隔符。

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.

如何在R中用逗号作为小数点分隔符和字段分隔符来读取数字?

推荐答案

根据您的阅读方式,可能可以使用 dec 参数文件中。这是我使用 data.table 的方式:

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

这篇关于R:如何使用逗号作为Dec分隔符&amp;来读取数字字段分隔符? “读取'dec'和'sep'的两个参数相等(',')。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆