我大约有300-500个CSV文件,其开头有一些字符信息,而两列则为数字数据。我想用所有数值制作一个data.frame,这样我就拥有了一次X列和多个Y,而没有字符行。
**File1** has two-column and more than a thousand rows: an example looks like
info info
info info
info info
X Y
1 50.3
2 56.2
3 96.5
4 56.4
5 65.2
info 0
**File2**
info info
info info
info info
X Y
1 46.3
2 65.2
3 21.6
4 98.2
5 25.3
info 0
Only Y values are changing from file to file, I want to add all the files in one file with selective rows and make a data frame. Such as I want as a data frame.
X Y1 Y2
1 46.3 50.3
2 65.2 56.2
3 21.6 96.5
4 98.2 56.4
5 25.3 65.2
I tried
files <- list.files(pattern="*.csv")
l <- list()
for (i in 1:length(files)){
l[[i]] <- read.csv(files[i], skip = 3)
}
data.frame(l)
This gives me
X1 Y1 X2 Y2
1 46.3 1 50.3
2 65.2 2 56.2
3 21.6 3 96.5
4 98.2 4 56.4
5 25.3 5 65.2
info 0 info 0
如何跳过最后一行和第X列仅作为第一列(因为X值不变)
答案 0 :(得分:0)
定义一个函数Read
,该函数读取一个文件,删除所有不以数字开头的行。我们使用sep=","
来指定每个文件中的字段以逗号分隔。然后将Read
与read.zoo
一起使用,以读取和合并给出动物园对象z
的文件。最后,如图所示,将其用作动物园对象或将其转换为数据框。
library(zoo)
Read <- function(f) {
read.table(text = grep("^\\d", readLines(f), value = TRUE), sep = ".")
}
z <- read.zoo(Sys.glob("*.csv"), read = Read)
DF <- fortify.zoo(z)