Re: [問題] 輸出的結果,如何完整貼到文件呢?(求救)

作者: TreeMan (好啊...)   2018-11-03 17:16:45
我用writeLines試著寫了一個, 應該是可以符合你的需求, 你可以再嘗試最佳化~
#示範用data frame
library(dplyr)
x <- c('A', 'B', 'C')
y <- c(1, 2, 3)
df <- data.frame(x, y) %>%
mutate_all(as.character)
#先設定一個檔案 (檔名改word也可以), 等會writeline可以寫入
text_print <- file("text_print.txt", open="wt")
ch.col<- names(df)[sapply(df, is.character)]
for (name in ch.col)
{
line1 <- sprintf("Unique value for %s is",name)
line2 <- unique(df[[name]])
writeLines(line1, con = text_print, sep = "\n", useBytes = FALSE)
writeLines(line2, con = text_print, sep = " ", useBytes = FALSE)
writeLines("\n", con = text_print, sep = "\n", useBytes = FALSE)
}
close(text_print)
#開啟檔案就可以看到寫入的資料了
作者: ronald736 (明天會更好)   2018-11-03 21:39:00
謝謝大大,我再試試看
作者: TreeMan (好啊...)   2018-11-03 22:19:00
啊 剛才發現, 那個paste0是測試時用的, 我把它拿掉~
作者: ronald736 (明天會更好)   2018-11-04 09:50:00
不過後來我有跑 paste0不影響 ㄧ樣可以匯出

Links booklink

Contact Us: admin [ a t ] ucptt.com