這是關于我在將資料匯出到.csv檔案時面臨的問題。我正在使用 R 中的一個大型資料框。它包含幾個字符大于32767的單元格,這顯然是一個單元格可以容納的最大值。當我將資料匯出到.csv檔案時,這些單元格的內容會溢位到下一行。然而,一旦將.csv檔案匯入 RStudio,資料框看起來就完全沒問題了。匯出資料時,有沒有辦法將每個單元格中的字符數限制為32767 ?
uj5u.com熱心網友回復:
將長字串拆分為n列,每列有x 個字符,例如:
# example data
d <- data.frame(x = c("longstring", "anotherlongstring"))
d
# x
# 1 longstring
# 2 anotherlongstring
x = 6
n = 3
res <- read.fwf(file = textConnection(d$x), widths = rep(x, n))
res
# V1 V2 V3
# 1 longst ring <NA>
# 2 anothe rlongs tring
uj5u.com熱心網友回復:
我在此處發布的答案中找到了我的問題的解決方案。
假設df是我要匯出到.csv檔案的資料框。以下代碼完成了這項作業
df <- as.data.frame(substr(as.matrix(df), 1, 32767))
write.csv(df, <file>, rownames = FALSE)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/466942.html
上一篇:使用Java流從csv檔案中過濾
