我使用fread来导入非常大的.CSV文件。一些列在文本之后有空格,我需要去掉这些空格。这个过程太耗费时间(几个小时)。
以下代码可以运行,但是system.time命令非常缓慢(在我的计算机上约为12秒,而实际文件要大得多)。
以下代码可以运行,但是system.time命令非常缓慢(在我的计算机上约为12秒,而实际文件要大得多)。
library(data.table)
library(stringr)
# Create example-data
df.1 <- rbind(c("Text1 ", 1, 2), c("Text2 ", 3, 4), c("Text99 ", 5, 6))
colnames(df.1) <- c("Tx", "Nr1", "Nr2")
dt.1 <- data.table(df.1)
for (i in 1:15) {
dt.1 <- rbind(dt.1, dt.1)
}
# Trim the "Tx"-column
dt.1[, rowid := 1:nrow(dt.1)]
setkey(dt.1, rowid)
system.time( dt.1[, Tx2 :={ str_trim(Tx) }, by=rowid] )
dt.1[, rowid:=NULL]
dt.1[, Tx:=NULL]
setnames(dt.1, "Tx2", "Tx")
有没有更快的方法在data.tables中去除空格?