2017-08-02 219 views
2

我正嘗試使用R中的Openxls讀取特定列。問題是來自列的數據不在一起。 以下是我的代碼:使用R中的openxlsx從excel表中讀取一列R

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)) 
} 

謝謝!

回答

1

默認情況下,colNames參數設置爲TRUE。將其設置爲FALSE,並且您的read.xlsx函數調用不會將第一行解釋爲列標題:

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