[問題] csv儲存格存成記事本txt檔

看板R_Language作者 (Nina)時間6年前 (2018/02/28 13:30), 編輯推噓2(2013)
留言15則, 4人參與, 6年前最新討論串1/1
[問題類型]: 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來) [軟體熟悉度]: 新手(沒寫過程式,R 是我的第一次) [問題敘述]: 有個csv檔(約三萬筆資料) no id time message 1 12345 2016-07-07T02:07:15+0000 Sounds great! 2 67890 2016-07-07T02:12:14+0000 Good good eat! 3 66666 2016-07-07T02:28:44+0000 I like it. . . . . . . . . 30000 34567 2017-11-01T09:47:53+0000 The end~ 我想做的是,把message那欄的每個儲存格都存成一個txt檔, 例如 Sounds great! >> 1.txt Good good eat! >> 2.txt I like it. >> 3.txt . . The end~ >> 30000.txt 請問應該怎麼做? 感謝各位大神了~ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.8.152.63 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1519795854.A.45C.html

02/28 13:41, 6年前 , 1F
for(i in 1:length(data$message)){
02/28 13:41, 1F

02/28 13:41, 6年前 , 2F
path = paste0('~/your path/', i, '.txt')
02/28 13:41, 2F

02/28 13:41, 6年前 , 3F
write.table(data$message[i], path)
02/28 13:41, 3F

02/28 13:41, 6年前 , 4F
}
02/28 13:41, 4F

02/28 13:59, 6年前 , 5F
樓上的write.table改成write可能比較合適
02/28 13:59, 5F

02/28 14:54, 6年前 , 6F
謝謝各位~~問題解決了!我最後是用write.table這個函數,
02/28 14:54, 6F

02/28 14:54, 6年前 , 7F
因為我用write的話輸出的記事本內容是"1" "2" "3"...,
02/28 14:54, 7F

02/28 14:54, 6年前 , 8F
不是我要的message內容,還是謝謝大家!
02/28 14:54, 8F

02/28 15:02, 6年前 , 9F
我比較好奇創造出三萬個TXT會怎麼樣
02/28 15:02, 9F

02/28 15:27, 6年前 , 10F
那是因為write的輸出跟輸入跟write.table相反= ="
02/28 15:27, 10F

02/28 15:28, 6年前 , 11F
我記錯了@@
02/28 15:28, 11F

02/28 15:28, 6年前 , 12F
喔喔,因為data$message是factor阿...
02/28 15:28, 12F

02/28 15:29, 6年前 , 13F
我會推薦write原因是因為write.table會輸出
02/28 15:29, 13F

02/28 15:29, 6年前 , 14F
rownames跟colnames,write就可以直接避免掉了
02/28 15:29, 14F

03/01 13:54, 6年前 , 15F
原來如此,謝謝c大指教!
03/01 13:54, 15F
文章代碼(AID): #1QbZwEHS (R_Language)