r中几个csv文件中的cbind列
cbind column in several csv files in r
我是 R 的新手,不知道如何进行 for 循环。
这是我的问题:我在一个文件夹中有大约 160 个 csv 文件,每个文件都有一个特定的名称。在每个文件中,都有一个模式:"HL.X.Y.Z.",其中 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)
问候,
莱恩
我是 R 的新手,不知道如何进行 for 循环。 这是我的问题:我在一个文件夹中有大约 160 个 csv 文件,每个文件都有一个特定的名称。在每个文件中,都有一个模式:"HL.X.Y.Z.",其中 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)
问候,
莱恩