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

看板R_Language作者 (好啊...)時間6年前 (2018/11/03 17:16), 6年前編輯推噓2(201)
留言3則, 2人參與, 6年前最新討論串2/2 (看更多)
我用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

11/03 21:39, 6年前 , 1F
謝謝大大,我再試試看
11/03 21:39, 1F

11/03 22:19, 6年前 , 2F
啊 剛才發現, 那個paste0是測試時用的, 我把它拿掉~
11/03 22:19, 2F
※ 編輯: TreeMan (1.173.79.18), 11/03/2018 22:19:50

11/04 09:50, 6年前 , 3F
不過後來我有跑 paste0不影響 ㄧ樣可以匯出
11/04 09:50, 3F
文章代碼(AID): #1RtMU0fi (R_Language)
文章代碼(AID): #1RtMU0fi (R_Language)