来自"data.table“的data.table能否强制成功地将"."用作sep值?
我试图使用fread来加快concat.split函数在“分叉式”中的速度。关于我所采用的一般方法,请参阅这个要旨,而我想要切换的原因请参见这个问题。
我遇到的问题是把一个点(".")作为sep的一个值。每当我这样做,我就会得到一个“意想不到的字符”错误。
下面的简化示例演示了这个问题。
library(data.table)
y <- paste("192.168.1.", 1:10, sep = "")
x1 <- tempfile()
writeLines(y, x1)
fread(x1, sep = ".", header = FALSE)
# Error in fread(x1, sep = ".", header = FALSE) : Unexpected character (
# 192) ending field 2 of line 1我在当前函数中的解决办法是用另一个字符替代".","|"说,这个字符希望在原始数据中不存在,但这对我来说很危险,因为我无法预测其他人的数据集中有什么。这是解决办法。
x2 <- tempfile()
z <- gsub(".", "|", y, fixed=TRUE)
writeLines(z, x2)
fread(x2, sep = "|", header = FALSE)
#      V1  V2 V3 V4
#  1: 192 168  1  1
#  2: 192 168  1  2
#  3: 192 168  1  3
#  4: 192 168  1  4
#  5: 192 168  1  5
#  6: 192 168  1  6
#  7: 192 168  1  7
#  8: 192 168  1  8
#  9: 192 168  1  9
# 10: 192 168  1 10为了解决这个问题,假设数据是平衡的(每一行都有相同数量的"sep“字符)。我知道使用"."作为分隔符并不是最好的主意,但我只是想说明其他用户在他们的数据集中可能拥有什么,基于这里的其他 问题 我已经回答了。
发布于 2014-11-12 21:22:04
现在在GitHub上用v1.9.5实现。
> input = paste( paste("192.168.1.", 1:5, sep=""), collapse="\n")
> cat(input,"\n")
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5 设置sep='.'会导致与新参数dec (默认情况下为'.')之间的歧义:
> fread(input,sep=".")
Error in fread(input, sep = ".") : 
  The two arguments to fread 'dec' and 'sep' are equal ('.')因此,请为dec选择其他内容:
> fread(input,sep=".",dec=",")
    V1  V2 V3 V4
1: 192 168  1  1
2: 192 168  1  2
3: 192 168  1  3
4: 192 168  1  4
5: 192 168  1  5你可能会收到警告:
> fread(input,sep=".",dec=",")
     V1  V2 V3 V4
 1: 192 168  1  1
 2: 192 168  1  2
 3: 192 168  1  3
 4: 192 168  1  4
 5: 192 168  1  5
Warning message:
In fread(input, sep = ".", dec = ",") :
  Run again with verbose=TRUE to inspect... Unable to change to a locale
  which provides the desired dec. You will need to add a valid locale name
  to getOption("datatable.fread.dec.locale"). See the paragraph in ?fread.忽略或取消警告,或阅读段落并设置选项:
options(datatable.fread.dec.locale = "fr_FR.utf8")这确保了不会有任何含糊不清之处。
发布于 2013-10-08 05:21:08
这个问题与文本本身的数值有关:
library(data.table)
y <- paste("Hz.BB.GHG.", 1:10, sep = "")
xChar <- tempfile()
writeLines(y, xChar)
fread(xChar, sep = ".", header = FALSE)
#     V1 V2  V3 V4
#  1: Hz BB GHG  1
#  2: Hz BB GHG  2
#  3: Hz BB GHG  3
#  4: Hz BB GHG  4
#  5: Hz BB GHG  5
#  6: Hz BB GHG  6
#  7: Hz BB GHG  7
#  8: Hz BB GHG  8
#  9: Hz BB GHG  9
# 10: Hz BB GHG 10但是,尝试使用原始值时,同样会出现相同的错误:
fread(x1, sep = ".", header = FALSE, colClasses="numeric", verbose=TRUE)
fread(x1, sep = ".", header = FALSE, colClasses="character", verbose=TRUE)
 Detected eol as \n only (no \r afterwards), the UNIX and Mac standard.
 Looking for supplied sep '.' on line 10 (the last non blank line in the first 'autostart') ... found ok
 Found 4 columns
 First row with 4 fields occurs on line 1 (either column names or first row of data)
 Error in fread(x1, sep = ".", header = FALSE, colClasses = "character",  : 
   Unexpected character (192.) ending field 2 of line 1然而,这样做是可行的:
read.table(x1, sep=".")
#     V1  V2 V3 V4
# 1  192 168  1  1
# 2  192 168  1  2
# 3  192 168  1  3
# 4  192 168  1  4
# ... <cropped>https://stackoverflow.com/questions/19239376
复制相似问题