Re: [問題] 如何讀入多個CSV檔

看板R_Language作者 (天)時間8年前 (2016/01/15 15:21), 8年前編輯推噓2(205)
留言7則, 1人參與, 最新討論串7/7 (看更多)
※ 引述《clansoda (小笨)》之銘言: : 感謝各位的幫助,我目前資料讀取沒甚麼問題了,是後面卡關了 : 我寫一個早上都沒辦法突破,我先把資料貼上來,因為用敘述的好像很難 : https://goo.gl/DpkiII,這是我目前用的資料 連結失效 : 要寫出一個能夠算出sulfate或nitrate的平均數的function : 我目前是想說用土法煉鋼的方式將資料讀進來然後用dplyr的函數來篩選 : 我寫成這樣pollutanmean<-function (directory, pollutan, id=1:332 ) { : csv_file<-vector("list",length(directory)) : for (i in seq_along(directory)) { : csv_file[[i]]<-read.csv(directory[[i]]) : } : s<-0 : do.call(rbind,csv_file)->completedata : library(dplyr) : final<-completedata %>% filter(ID==id) %>% : select("pollutan") : s+mean(final,na.rm=TRUE)->s : return(s) library(data.table) library(plyr) library(dplyr) library(magrittr) pollutanmean <- function(csvFilesLoc, pollutan, id = 1:332){ # other method for reading data # sprintf('%s/%03i.csv', csvFilesLoc, id) %>% llply(fread) %>% bind_rows %>% list.files(csvFilesLoc, full.names = TRUE, pattern = '\\.csv') %>% {llply(.[id], fread)} %>% bind_rows %>% summarise_(.dots = paste0("mean(", pollutan, ", na.rm = TRUE)")) %>% unlist %>% set_names(NULL) %>% return } # sulfate, nitrate pollutanmean('specdata', 'nitrate', 1:20) # 0.8286293 pollutanmean('specdata', 'sulfate', 70:80) # 2.712632 : 我有碰到一個問題是我想用read.csv只讀我想要的CSV檔案就好了 : 但是list[[id]]這樣的寫法會有問題,應該是裡面只能放一個數字 : 假設id是1:10這樣的話就會有問題,然後我就想是不是可以用for (i in seq_along(id)) seq_along是沿著id長度,所以下面要用id,要使用id[i] 不然就是直接 for (i in id) : 這樣的方式去讀,可是假設使用者將id填入68:70,我發現seq_along會傳回1、2、3 : 這樣的數字,所以我只好把所有CSV檔案都讀進來然後用昨天學到的do.call將所有 : 檔案合併然後用filter篩選ID為使用者填入的id,接著選擇sulfate或nitrate : 其中一個變數然後做平均接著傳回來 : 我得到下面的error提示 : Error: All select() inputs must resolve to integer column positions. select不能用字串,請用select_ : The following do not: : * "pollutan" : In addition: Warning message: : In c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, : : longer object length is not a multiple of shorter object length : 第一個我覺得是說要叫我指定變數的位置,但是我學到的select的直接放 : 我想要留下來的變數名稱就好了,第二個我就真的看不懂他在寫什麼了 : 想請問我中間的邏輯哪邊有錯,不好意思一直在同一個問題打轉。 -- R資料整理套件系列文: magrittr #1LhSWhpH (R_Language) http://tinyurl.com/1LhSWhpH data.table #1LhW7Tvj (R_Language) http://tinyurl.com/1LhW7Tvj dplyr(上) #1LhpJCfB (R_Language) http://tinyurl.com/1LhpJCfB dplyr(下) #1Lhw8b-s (R_Language) tidyr #1Liqls1R (R_Language) http://tinyurl.com/1Liqls1R -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.109.74.87 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1452842512.A.F9A.html

01/15 15:53, , 1F
我把資料丟到googledrive上了 https://goo.gl/zJygna
01/15 15:53, 1F
測試完成

01/15 19:50, , 2F
大大我回在這篇文
01/15 19:50, 2F

01/15 19:50, , 3F
我現在寫到幾乎完成了 剩下for (i in id)這個環節
01/15 19:50, 3F

01/15 19:51, , 4F
我發現如果我這樣寫read.csv(path[i]) 他只會讀入
01/15 19:51, 4F

01/15 19:51, , 5F
最後一個數字代表的CSV欸,比如id為1:10 那就只會讀入
01/15 19:51, 5F

01/15 19:51, , 6F
010號CSV,導致最後的結果是錯的,想請問for id 這種寫
01/15 19:51, 6F

01/15 19:51, , 7F
法是對的嗎?
01/15 19:51, 7F
我不知道你path放什麼,但是我確定id = 2:5; for (i in id) cat(i, '\n') 會出現 2 3 4 5 ※ 編輯: celestialgod (140.109.73.232), 01/15/2016 20:41:41
文章代碼(AID): #1Mc9uG-Q (R_Language)
討論串 (同標題文章)
文章代碼(AID): #1Mc9uG-Q (R_Language)