2015-12-05 57 views
2

我需要下載300多個可在線的.csv文件,並將它們合併到R的數據框中。它們都有相同的列名,但長度(行數)不同。將在線的.csv文件合併到R的數據框中

l<-c(1441,1447,1577) 
s1<-"https://coraltraits.org/species/" 
s2<-".csv" 

for (i in l){ 
    n<-paste(s1,i,s2, sep="") #creates download url for i 
    x <- read.csv(curl(n)) #reads download url for i 
    #need to sucessively combine each of the 3 dataframes into one 
} 
+2

'read.csv'接受網址:'TMP < - do.call('rbind ',tmp1 < - Vectorize(read.csv,SIMPLIFY = FALSE)(paste0(s1,l,s2)))'其中'tmp1'將您的數據框列表和'tmp'組合爲 – rawr

回答

1

如果他們有相同的列,那麼它只是附加行的問題。一個簡單的(但不是記憶效率)的方法是使用rbind在循環

l<-c(1441,1447,1577) 
s1<-"https://coraltraits.org/species/" 
s2<-".csv" 

data <- NULL 
for (i in l){ 
    n<-paste(s1,i,s2, sep="") #creates download url for i 
    x <- read.csv(curl(n)) #reads download url for i 
    #need to sucessively combine each of the 3 dataframes into one 
    data <- rbind(data,x) 
} 

更有效的辦法是建立一個列表,然後將它們組合成在最後一個數據幀,但我會離開這個作爲你的鍛鍊。

+1

「但I將作爲你的練習。「大聲笑。相反,請在答案中顯示它。 –

+0

感謝您的解決方案Rohit。你的解決方案的工作原理,但是如你所說這是一個內存效率低下的問題。我有370個在線csv文件,每個大約30 x 200)結合起來。因此可能需要考慮列表選項。 – Elizabeth

5

就像@RohitDas所說的,不斷追加數據幀效率很低,速度會很慢。只需將每個csv文件下載爲列表中的條目,然後在收集列表中的所有數據後綁定所有行。

l <- c(1441,1447,1577) 
s1 <- "https://coraltraits.org/species/" 
s2 <- ".csv" 

# Initialize a list 
x <- list() 

# Loop through l and download the table as an element in the list  
for(i in l) { 
    n <- paste(s1, i, s2, sep = "") # Creates download url for i 
    # Download the table as the i'th entry in the list, x 
    x[[i]] <- read.csv(curl(n)) # reads download url for i 
} 

# Combine the list of data frames into one data frame 
x <- do.call("rbind", x) 

只是一個警告:x中的所有數據幀都必須具有相同的列來執行此操作。如果x中的一個條目具有不同數量的列或不同名稱的列,則rbind將失敗。

更高效的行綁定函數(帶有一些額外功能,如列填充)存在於幾個不同的包中。看看這些解決方案的結合行:

  • plyr::rbind.fill()
  • dplyr::bind_rows()
  • data.table::rbindlist()
相關問題