简体   繁体   English

将多个 csv 文件(类似列)与 r 中的选择性行和列合并为一个

[英]Combine multiple csv files (similar column) in one with selective row and column in r

I have around 300-500 CSV files with some character information at the beginning and two-column as numeric data.我有大约 300-500 个 CSV 文件,开头有一些字符信息,两列作为数字数据。 I want to make one data.frame with all the numeric values in Such a way that I have column X once with multiple Y without the character rows.我想用这样的方式制作一个包含所有数值的 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

How can I skip the last row and column X as the first column only (since X values do not change)如何仅将最后一行和 X 列作为第一列跳过(因为 X 值不会改变)

Define a function Read that reads one file removing all lines that do not start with a digit.定义一个函数Read读取一个文件,删除所有不以数字开头的行。 We use sep="," to specify that the fields in each file are comma separated.我们使用sep=","来指定每个文件中的字段以逗号分隔。 Then use Read with read.zoo to read and merge the files giving zoo object z .然后使用Readread.zoo来读取和合并给出 zoo 对象z的文件。 Finally either use it as a zoo object or convert it to a data frame as shown.最后,要么将其用作动物园对象,要么将其转换为数据框,如图所示。

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写一个 R 循环来添加多个 excel 文件,更改它们的列名,然后基于一行组合它们? - How to write a R loop to add multiple excel files, change their column names, and then combine them based on one row? 在第1列中合并2个相似的行,并在r中合并第二个列文本 - combine 2 similar row in column 1 and merge second column text in r R合并多个.csv文件,并在另一列中保留源csv文件名 - R Combine multiple .csv files and retain source csv file name in an additional column 在 R 中将多个竞赛列合并为一列 - Combine multiple race columns into one column in R 有一个 .csv 文件的文件夹,想将它们组合成 R 中的一个数据帧,并使用每个文件名作为列标题 - have a folder of .csv files, would like to combine them into one dataframe in R and use each filename as a column header 如何组合多个.csv文件,并在R中添加每个数据集名称的列? - How to combine multiple .csv files, and add a column with each dataset's name, in R? 将多个 CSV 文件导入 R 并将前两行合并为 header 列 - Import multiple CSV files into R and combine first two rows as the header column 阅读csv的第四列,并在r中合并为一个文件 - Read Fourth column of csv and combine into one file in r 从R中的多个文件中读取一列 - Read one column from multiple files in r 使用 R 只选择一列批量处理多个 .csv 文件 - Batch processing multiple .csv files using R selecting only one column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM