简体   繁体   English

如何从工作表中选择特定单元格并使用 R 组装一个新的数据框

[英]How to select specific cells from worksheets and assemble a new dataframe with R

I have dozens of Excel sheets, with standardized names and in the R project folder, and I would like to select specific cells, with standardized locations between the sheets and assemble a new data frame.我有几十个 Excel 工作表,在 R 项目文件夹中具有标准化的名称,我想选择特定的单元格,在工作表之间具有标准化的位置并组装一个新的数据框。

Example: In cell A3, I have name;示例:在单元格 A3 中,我有名字; In cell C5, I have age;在 C5 单元格中,我有年龄; In cell F4, I have a profession;在 F4 单元格中,我有一个职业; In cell F10 I have the city;在 F10 单元格中,我有城市; In cell J22, I have an Opinion about a product.在单元格 J22 中,我对产品有意见。 How can I build a new dataframe that shows me the columns:如何构建一个向我显示列的新数据框:

NAME AGE PROFESSION CITY OPINION姓名 年龄 职业 城市 意见

and in each row the data extracted from each worksheet that is in the folder?并在每一行中从文件夹中的每个工作表中提取数据?

Thank you very much in advance.非常感谢您提前。

For example, the Excel sheets (ie saved as csv file) are at your current working directory:例如,Excel 工作表(即保存为 csv 文件)位于您当前的工作目录中:

Reading csv files in R, the first column is index, so A corresponding to the second column, B -> the third, and so on...在R中读取csv文件,第一列是索引,所以A对应第二列,B->第三列,以此类推...

=> A3 -> [3, 2] => A3 -> [3, 2]

C5 -> [5, 4] C5 -> [5, 4]

F4 -> [4, 7] F4 -> [4, 7]

F10 -> [10, 7] F10 -> [10, 7]

J22 -> [22, 11] J22 -> [22, 11]

You can do the following:您可以执行以下操作:

files <- list.files(".")
df <- data.frame(matrix(NA, nrow=length(files),
                        ncol=5))
colnames(df) <- c("NAME", "AGE", "PROFESSION", "CITY", "OPINION")

for(i in 1:length(files)){
  raw_read <- read.csv(files[i])
  df[i,] <- c(raw_read[3,2],
              raw_read[5,4],
              raw_read[4,7],
              raw_read[10,7],
              raw_read[22,11])
}

Change read.csv() to other function for other formats of file. read.csv()更改为其他文件格式的其他函数。

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

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