简体   繁体   English

从 R 中的大型 Excel 文件读取/导入特定行

[英]Read / Import specific rows from large Excel files in R

I have dozens of very heavy Excel files that I need to import into R (then rebind).我有几十个非常重的 Excel 文件需要导入到 R(然后重新绑定)。 Each file has 2 sheets, where the second sheet (name: "Results") consists of 100K rows at least and has about 350 columns.每个文件有 2 个工作表,其中第二个工作表(名称:“结果”)至少包含 100K 行,大约有 350 列。

I would like to read a subset of the sheet "Results" from each file by columns, but most importantly, by specific rows.我想按列从每个文件中读取工作表“结果”的一个子集,但最重要的是,按特定行读取。 Each "ID" in the data, has a main row and then multiple rows below which contain data in specific columns.数据中的每个“ID”都有一个主行,然后是下面的多行,其中包含特定列中的数据。 I would like to read the main row only (this leaves each file with 50-400 rows (depending on the file) and 150 variables).我只想读取主行(这使每个文件有 50-400 行(取决于文件)和 150 个变量)。 The first column that numbers main rows does not have a header.对主要行进行编号的第一列没有 header。

This is what the data looks like (simplified):这是数据的样子(简化): 在此处输入图像描述

I would like to import only the rows whose first column isn't empty but numbered (ie, 1., 13., 34., 211.) and particular columns, in this example columns 2,3,5 (ie, name, ID, status).我只想导入第一列不为空但已编号(即 1.、13.、34.、211.)的行和特定列,在此示例中为第 2、3、5 列(即名称、身份、状态)。 The desired output would be:所需的 output 将是:

在此处输入图像描述

Is there a simple way to do this?有没有简单的方法可以做到这一点?

Let's say a is our excel file, as data frame.假设a是我们的 excel 文件,作为数据框。

library(readxl)
a <- as.data.frame(read_excel("Pattern/File.xlsx",sheet = "Results"))

For instance, we want to select columns 1 to 3, so use例如,我们想要 select 列 1 到 3,所以使用

subset(a[,1:3],is.na(a[1])==FALSE)

By this function, you are subsetting the input data frame with values different than NA in first column.通过这个 function,您正在使用不同于第一列中的 NA 的值对输入数据框进行子集化。

Output: Output:

  ...1 name   ID
1    1  Dan us1d
4   13  Nev sa2e
6   34  Sam il5a

Note first column name ("...1 ").注意第一列名称(“...1”)。 This is autogenerated by read_excel() function, but should not be a problem.这是由read_excel() function 自动生成的,但应该不是问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM