簡體   English   中英

R中幾個csv文件中的cbind列

[英]cbind column in several csv files in r

我是R的新手,也不知道確切地執行循環。 這是我的問題:文件夾中大約有160個csv文件,每個文件都有一個特定的名稱。 在每個文件中,都有一個模式:“ HL.XYZ”,其中X =“ Region”,Y =“ cluster”和Z =“ point”。 我需要做的是讀取所有這些csv文件,從名稱中提取字符串,使用每個csv文件的字符串創建一列,並將所有這些csv文件綁定在一個數據幀中。 這是我正在嘗試做的一些代碼:

setwd("C:/Users/worddirect")
files.names<-list.files(getwd(),pattern="*.csv")
files.names 
head(files.names)
>[1] "HL.1.1.1.2F31CA.150722.csv"  "HL.1.1.2.2F316A.150722.csv" 
 [3] "HL.1.1.3.2F3274.150722.csv"  "HL.1.1.4.2F3438.csv"        
 [5] "HL.1.10.1.3062CD.150722.csv" "HL.1.10.2.2F343D.150722.csv"

這樣做可以讀取所有文件:

files.names
    for (i in 1:length(files.names)) {
    assign(files.names[i], read.csv(files.names[i],skip=18))
            }

像這樣為單個csv文件添加額外的列可以正常工作:

test<-cbind("Region"=rep(substring(files.names[1],4,4),times=nrow(HL.1.1.1.2F31CA.150722.csv)),
        "Cluster"=rep(substring(files.names[1],6,6),times=nrow(HL.1.1.1.2F31CA.150722.csv)),
        "Point"=rep(substring(files.names[1],8,8),times=nrow(HL.1.1.1.2F31CA.150722.csv)),
        HL.1.1.1.2F31CA.150722.csv)
 head(test)
  Region Cluster Point          Date.Time Unit  Value
1      1       1     1 6/2/14 11:00:01 PM    C 24.111
2      1       1     1  6/3/14 1:30:01 AM    C 21.610
3      1       1     1  6/3/14 4:00:01 AM    C 20.609

但是,上面的for循環不起作用。

files.names
    for (i in 1:length(files.names)) {
    assign(files.names[i], read.csv(files.names[i],skip=18))
    cbind("Region"=rep(substring(files.names[i],4,4),times=nrow(i)),
        "Cluster"=rep(substring(files.names[i],6,6),times=nrow(i)),
        "Point"=rep(substring(files.names[i],8,8),times=nrow(i)),
        i)
            }
>Error in rep(substring(files.names[i], 4, 4), times = nrow(i)) : 
  invalid 'times' argument

最后一步是將所有csv文件綁定在一個數據幀中。

我感謝任何建議。 如果有任何更簡單的方式來做我所做的事,我也很感謝!

i是數字,沒有nrow屬性。

您可以使用以下代碼

result = data.frame()

for (i in 1:length(files.names)) {
    assign(files.names[i], read.csv(files.names[i],skip=18))
    result = rbind(
                  cbind(
                      "Region"=rep(substring(files.names[i],4,4),times=nrow(files.names[i])),
                      "Cluster"=rep(substring(files.names[i],6,6),times=nrow(files.names[i])),
                      "Point"=rep(substring(files.names[i],8,8),times=nrow(files.names[i])),
                      files.names[i]))
}

解決R中的問題的方法有很多。更類似於R的方法是使用apply()函數。 apply()系列函數的作用類似於隱式的for循環,對通過函數參數傳遞給它的每個項目應用一個或多個操作。

R的另一個重要特征是匿名函數。 結合使用lapply()和匿名函數,我們可以解決您的多文件讀取問題。

setwd("C:/Users/worddirect")
files.names<-list.files(getwd(),pattern="*.csv")
# read csv files and return them as items in a list()
theList <- lapply(files.names,function(x){
     theData <- read.csv(x,skip=18)
     # bind the region, cluster, and point data and return
     cbind(
          "Region"=rep(substring(x,4,4),times=nrow(theData)),
          "Cluster"=rep(substring(x,6,6),times=nrow(theData)),
          "Point"=rep(substring(x,8,8),times=nrow(theData)),
          theData)
})
# rbind the data frames in theList into a single data frame  
theResult <- do.call(rbind,theList)

問候,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM