使用 R 中的 openxlsx 从 Excel 工作表中读取列

标签 r excel multiple-columns

我正在尝试使用 R 中的 Openxls 读取特定列。问题是该列中的数据不在一起。 以下是我的代码:

library("openxlsx")
excel0111 <- loadWorkbook("C:\\path\\0111F.xlsx")
sheet_names <- names(excel0111)
c0111 <- read.xlsx(excel0111, sheet = sheet_names[1],cols = 2, rows = c(4:27)) # reading data from (B4:B27)

但是输出看起来像这样:

    213
1   188
2   183
3   260
4   389
5   680
6  1614
7  2044
8  1544
9  1261
10 1040
11  991
12  999
13 1045
14 1133
15 1183
16 1238
17 1334
18 1122
19  690
20  607
21  524
22  366
23  390

我想要的输出如下:

1   213
2   188
3   183
4   260
5   389
6   680
7  1614
8  2044
9  1544
10  1261
11 1040
12  991
13  999
14 1045
15 1133
16 1183
17 1238
18 1334
19 1122
20  690
21  607
22  524
23  366
24  390

最终,我将使用循环从 Excel 文件的所有工作表中获取相同的列,如下所示:

for (i in 1:212) {


   c0111[i] <- read.xlsx(excel0111, sheet = sheet_names[i], skipEmptyRows = FALSE,cols = c(2), rows = c(4:27))
}

谢谢!

最佳答案

默认情况下,colNames 参数设置为 TRUE。将其设置为 FALSE,您的 read.xlsx 函数调用不会将第一行解释为列标题:

c0111 <- read.xlsx(excel0111, sheet = sheet_names[1], colNames = FALSE, cols = 2, rows = c(4:27))

关于使用 R 中的 openxlsx 从 Excel 工作表中读取列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45450239/

相关文章:

html - 如何在 knitr 制作的 HTML5 幻灯片中使用 "R-generated"图作为半透明背景?

r - 多个型号: how to select best model and make prediction

excel - 将 Google 搜索导出到电子表格

java - Excel,getStringCellValue 返回 float

html - 使用 CSS 在 div 中创建 'bottom tab'?

R从DF创建JSON列表

python - 在 conda 环境中在 MacOS 上导入 R 库时如何解释和修复 'shared object not found' 错误?

database - 我应该为数据库使用什么?

html - Bootstrap 列未正确对齐。

mysql - 从文本/Excel/CSV 在 MySQL 表中创建列