看板 R_Language 關於我們 聯絡資訊
我用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) #開啟檔案就可以看到寫入的資料了 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.173.79.18 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1541236608.A.A6C.html
ronald736: 謝謝大大,我再試試看 11/03 21:39
TreeMan: 啊 剛才發現, 那個paste0是測試時用的, 我把它拿掉~ 11/03 22:19
※ 編輯: TreeMan (1.173.79.18), 11/03/2018 22:19:50
ronald736: 不過後來我有跑 paste0不影響 ㄧ樣可以匯出 11/04 09:50