read.data <- function(x) {
## Define a function name read.data() to input all data.
i.x <- 1:length(x)
## Use vector x 's index as loop number.
for(i in i.x) {
m.i <- formatC(x, flag = 0, width = 3)
## Change the i to a charactor with 3 digitals width,
## for example: 1 to "001"."
d.m.i <- paste(m.i, "csv", sep = ".")
## Change the m.i to file name,
## for example:002 to 002.csv.
pl.d.i <- read.table(d.m.i, header = TRUE, sep = ",")
pl.data <<- rbind(pl.data, pl.d.i)
}
}
read.data <- function(x) {
## Define a function name read.data() to read all data.
m.x <- formatC(x, flag = 0, width = 3)
## Change the i to a charactor with 3 digitals width,
## for example: 1 to "001"."
d.m.x <- paste(m.x, "csv", sep = ".")
## Change the m.i to file name,
## for example:002 to 002.csv.
dat1 <<- do.call(rbind,lapply(d.m.x,read.csv))
}
read.data <- function(x) {
## Define a function name read.data() to read all data.
i.x <- 1:length(x) ## Use vector x's index as loop number.
pl.data <- data.frame()
## 定义一个空数据框,因为后面
## for循环中要用到这个变量
for(i in i.x) {
m.i <- formatC(x, flag = 0, width = 3)
## Change the i to a charactor with 3 digitals width,
## for example: 1 to "001"."
d.m.i <- paste(m.i, "csv", sep = ".")
## Change the m.i to file name,
## for example:002 to 002.csv.
pl.d.i <- read.table(d.m.i, header = TRUE, sep = ",",
stringsAsFactors = FALSE)
pl.data <- rbind(pl.data, pl.d.i)
## 1楼代码主要出在这一句,用的是超级赋值<<-,这样pl.data就不存在for函数内,而存在于全球环境中了。
}
return(pl.data) ## 关键是这一句,不能在for函数内
}