以下代碼生成資料檔案,其中每行具有不同的列數。該選項fill=TRUE似乎僅在達到特定字符限制時才起作用。例如,比較第 1-3 行和第 9-11 行,注意這兩個示例都按預期作業。我如何才能閱讀全部notworking1.datwith fill=TRUEenabled 而不僅僅是前 100 行?
for (i in seq(1000,1099,by=1))
cat(file="working1.dat", c(1:i, "\n"), append = TRUE)
df <- fread(input = "working1.dat", fill=TRUE)
for (i in seq(1000,1101,by=1))
cat(file="notworking1.dat", c(1:i, "\n"), append = TRUE)
df <- fread(input = "notworking1.dat", fill=TRUE)
for (i in seq(1,101,by=1))
cat(file="working2.dat", c(1:i, "\n"), append = TRUE)
df <- fread(input = "working2.dat", fill=TRUE)
以下解決方案也會失敗
df <- fread(input = "notworking1.dat", fill=TRUE, col.names=paste0("V", seq_len(1101)))
收到警告訊息:
Warning message: In data.table::fread(input = "notworking1.dat", fill = TRUE) : Stopped early on line 101. Expected 1099 fields but found 1100. Consider fill=TRUE and comment.char=. First discarded non-empty line: <<1 2 3 4 ...
uj5u.com熱心網友回復:
我們可以找出最大列數并添加那么多列,然后fread:
x <- readLines("notworking1.dat")
myHeader <- paste(paste0("V", seq(max(lengths(strsplit(x, " ", fixed = TRUE))))), collapse = " ")
# write with headers
write(myHeader, "tmp_file.txt")
write(x, "tmp_file.txt", append = TRUE)
# read as usual with fill
d1 <- fread("tmp_file.txt", fill = TRUE)
# check output
dim(d1)
# [1] 102 1101
d1[100:102, 1101]
# V1101
# 1: NA
# 2: NA
# 3: 1101
但是由于我們已經使用readLines匯入了資料,我們可以決議它:
x <- readLines("notworking1.dat")
xSplit <- strsplit(x, " ", fixed = TRUE)
# rowbind unequal length list, and convert to data.table
d2 <- data.table(t(sapply(xSplit, '[', seq(max(lengths(xSplit))))))
# check output
dim(d2)
# [1] 102 1101
d2[100:102, 1101]
# V1101
# 1: <NA>
# 2: <NA>
# 3: 1101
這是一個已知問題GitHub issue 5119,未實作,但建議fill也將整數作為輸入。所以解決方案是這樣的:
d <- fread(input = "notworking1.dat", fill = 1101)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/468526.html
上一篇:如何進行自定義分組依據?
下一篇:如何獲得R資料幀中的差異總和
