简体   繁体   English

如何在R中读取excel文件中的特定行

[英]How to read specific rows in excel file in R

I have an excel file .xlsx from which I want to read BE71:CZ71 and BE76:CZ76 and want to append these as columns to a pre-existing data frame.我有一个.xlsx excel 文件,我想从中读取BE71:CZ71BE76:CZ76并希望将它们作为列附加到预先存在的数据框中。 I could not find any argument equivalent to startRow such as "startColumn" in read.xlsx.我在 read.xlsx 中找不到与 startRow 等效的任何参数,例如“startColumn”。 I was hoping to use something like read.xlsx(<file_name>,startRow=3, startColumn="BE"..) but there is nothing such as startColumn.我希望使用read.xlsx(<file_name>,startRow=3, startColumn="BE"..)东西,但没有像 startColumn 这样的东西。 How do I pick up a row starting from a particular column and put it in a vector or a list which I can transpose to "cbind" to a data frame?如何从特定列开始选取一行并将其放入可以转置为“cbind”到数据框的向量或列表中? An example in which an excel file has 10 rows and 10 columns and something is read from the 5th row from the 4th column onwards would work for me to understand how to do the original problem.一个 excel 文件有 10 行和 10 列并且从第 4 列的第 5 行开始读取某些内容的示例将有助于我了解如何解决原始问题。 Thank you.谢谢你。

This should work:这应该有效:

library(readxl)

df1 <- read_excel(path, range = "BE71:CZ71")
df2 <- read_excel(path, range = "BE76:CZ76")

df_final <- cbind(as.data.frame(df1,df2,...)

To solve your example problem (a 10x10 matrix initialized at the top left of the excel sheet):要解决您的示例问题(在 Excel 表左上角初始化的 10x10 矩阵):

library(readxl)
excel_example <- read_excel("~/Desktop/excel_example.xlsx")
fifthrow_fourthcolumn_onwards <- as.matrix(excel_example[5:10, 4:10])
library(openxlsx)  
a<-read.xlsx("<filename>",startRow=3,colNames=TRUE)
p<-as.data.frame(t(a[c(69,74),-c(1:56,105:263)]),row.names = FALSE) 

-c(1:56.105:263) drops columns up to BD (56), and from DA (105) through to the last populated column in the sheet. -c(1:56.105:263)将列删除到BD (56),从DA (105) 到工作表中最后填充的列。 c(69,74) chooses rows 71 and 76 relative to startRow=3 . c(69,74)选择相对于startRow=3 71 和 76 行。

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

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