有没有一种更快的方法将字符串分割成给定长度的子字符串?

3

我有一些天气数据,列的宽度是固定的,但长度取决于变量(参见下面来自GHCN的数据,http://www1.ncdc.noaa.gov/pub/data/ghcn/daily/readme.txt)。

我想将它们拆分成数据框,并编写了一些代码,遵循@GSee的建议(如何将字符串分割为给定长度的子字符串?)。但处理6000行需要大约4.3秒。

有没有更快的方法处理这个数据集?

感谢任何建议。

temp <- readLines(textConnection("NO000050550193801TMAX   53  I   51  I   10  I   22  I   56  I   31  I   30  I   24  I   38  I   25  I    2  I   32  I   75  I   71  I   98  I   96  I   57  I   55  I   54  I   60  I   91  I   75  I   94  I   82  I   89  I   46  I   26  I   68  I   62  I   46  I   37  I
NO000050550193801TMIN   25  I   -6  I  -27  I    0  I    3  I  -14  I   -8  I   11  I   10  I  -11  I  -30  I  -23  I   22  I   38  I   47  I   33  I   13  I    5  I   10  I   29  I   42  I   45  I   51  I   44  I   35  I    5  I  -16  I  -20  I    5  I    2  I    5  I
NO000050550193802TMAX   69  I   58  I   71  I   90  I   77  I   70  I   56  I   46  I   58  I   32  I   32  I   22  I   25  I   30  I   29  I   29  I   34  I   88  I   58  I   50  I   45  I   62  I   38  I   40  I   59  I  112  I   92  I   77  I-9999   -9999   -9999   
NO000050550193802TMIN   11  I   26  I   16  I   35  I   44  I   21  I   19  I   22  I   20  I    6  I    6  I  -16  I  -22  I  -39  I  -28  I  -35  I  -33  I  -21  I  -13  I   15  I   26  I   17  I   -1  I    9  I   18  I   38  I   58  I   28  I-9999   -9999   -9999   
NO000050550193803TMAX   81  I   84  I   89  I   86  I   86  I   74  I   54  I   74  I   83  I   64  I   75  I   77  I   66  I   91  I   82  I   84  I   89  I   84  I   94  I   85  I   82  I   89  I   74  I   84  I   81  I   58  I   72  I   58  I   86  I   84  I   89  I
NO000050550193803TMIN   31  I   25  I   29  I   45  I   61  I   20  I    9  I    8  I   38  I   31  I    9  I   39  I   27  I   56  I   48  I   65  I   45  I   54  I   46  I   42  I   43  I   36  I   56  I   61  I   15  I   -2  I  -11  I   -2  I   12  I   30  I   24  I"))

temp <- rep(temp, 1000)
system.time({

out <- strsplit(temp, '')
out <- as.matrix(do.call(rbind, out))
pos_matrix <- matrix(c(12, 16, 18, seq(0, 30) * 8 + 22,
    15, 17, 21, seq(0, 30) * 8 + 26), ncol = 2)
out <- apply(out, 1, function(x)
    {
        apply(pos_matrix, 1, function(y) 
            paste(x[y[1]:y[2]], collapse = ''))
    })
})

user  system elapsed 
4.46    0.01    4.52 

编辑:Ananda Mahto 的评论:

system.time({
pos_matrix <- matrix(c(12, 16, 18, seq(0, 30) * 8 + 22,
    15, 17, 21, seq(0, 30) * 8 + 26), ncol = 2)
pos_matrix <- lapply(seq(1, nrow(pos_matrix)), function(x) 
    {
        sprintf('substr(V1, %s, %s) f%s',
            pos_matrix[x,1], pos_matrix[x,2], x)
    })
pos_matrix <- paste(pos_matrix, collapse = ', ')
out <- data.frame(V1 = temp)

out <- sqldf(sprintf('select %s from out', pos_matrix))
})

user  system elapsed 
 0.4     0.0     0.4 

根据jlhoward的建议修改:

system.time({
pos_matrix <- matrix(c(12, 16, 18, seq(0, 30) * 8 + 22,
    15, 17, 21, seq(0, 30) * 8 + 26), ncol = 2)
out <- apply(pos_matrix, 1, function(x)
    {
        substr(temp, x[1], x[2])
    })
})
user  system elapsed 
0.04    0.00    0.04

1
此处的示例6f中所述,使用sqldfsubstr - A5C1D2H2I1M1N2O1R2T1
sqldf和substr更快。对于相同的数据集,仅需要0.4秒。您可以在答案中添加您的评论,然后我可以接受它。 - Bangyou
3个回答

2

对您的代码进行剖析 (?Rprof) 显示,执行时间的三分之二花费在了 paste(...) 上,这并不奇怪。看起来您正在将输入分解为单个字符,然后基于 pos_matrix(...) 重新组装它们。使用具有起始位置和长度的矩阵以及 substr(...) 可能更有效率。

编辑: 添加实现上述建议的代码

vec <- as.vector(temp)
pos_matrix <- matrix(c(12, 16, 18, seq(0, 30) * 8 + 22,
                       15, 17, 21, seq(0, 30) * 8 + 26), ncol = 2)
pos <- t(pos_matrix)
system.time(
out <- do.call(rbind,list(apply(pos,2,function(x){substr(vec,x[1],x[2])})))
)
#   user  system elapsed 
#   0.09    0.00    0.09 

感谢您的建议。对于相同的数据集,只需要0.4秒,并且速度与sqldf类似(但不需要加载sqldf包)。 - Bangyou
很高兴它对你有用。我已经添加了代码,但看起来你已经弄清楚了。 - jlhoward

1

在默认加载的utils包中有一个固定宽度的读取函数:

m <- matrix(c(12, 16, 18, seq(0, 30) * 8 + 22,
     15, 17, 21, seq(0, 30) * 8 + 26), ncol = 2)
read.fwf(textConnection(temp), c(11,             # which you are apparently ignoring
                                 m[,2]-m[,1]+1)  )

但对我来说,6000条记录只需9秒钟。


0

scan -- 可以与文件或连接一起使用。可以修改您的代码,使其与上面给出的temp更方便:

writeLines(temp, "temp.txt")
scan("temp.txt", what=""))
# and now convert it to a matrix of appropriate size

不确定它是否比基于sqldf的解决方案更快,但对我来说看起来更直观。

[[注意]]好的,你问了关于“给定长度的子字符串”的问题,所以从技术上讲,我的答案是关于其他事情的。但看起来这可能实际上对像这个例子中的文件有帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接