我有一個匯入到 R 中的 xlsx 評論串列。它是一個串列串列,其中每個評論中存在的元素之一是表示評論在 Excel 中的位置的字串。我想將其表示為 [X,Y] 數字索引,就像在 R 中所做的那樣。
list_of_comments
$ :List of 2
..$ location : chr "BA5"
..$ content : chr "some content"
$ :List of 2
you get the picture
我已經嘗試通過創建預定義單元名稱的 data.frame 以硬編碼的方式進行操作。根據匹配的內容,將回傳索引。我很快意識到我什至不知道如何將它創建到雙字符領域(例如 AA2)。即使我這樣做了,我也會留下大量的data.frame。
是否有將 Excel 單元格位置轉換為行列索引的智能方法?
uj5u.com熱心網友回復:
您可以在這里使用cellranger幫助電源的軟體包readxl,特別是as.cell_addr()功能:
library(cellranger)
library(dplyr)
list_of_comments <- list(list(location = "BG5", content = "abc"),
list(location = "AA2", content = "xyz"))
bind_rows(list_of_comments) %>%
mutate(as.cell_addr(location, strict = FALSE) %>%
unclass() %>%
as_tibble())
# A tibble: 2 x 4
location content row col
<chr> <chr> <int> <int>
1 BG5 abc 5 59
2 AA2 xyz 2 27
uj5u.com熱心網友回復:
在基礎 R 中,您可以執行以下操作:
excel <- c("F3", "BG5")
r_rows <- as.numeric(sub("^.*?(\\d )$", "\\1", excel))
r_cols <- sapply(strsplit(sub("^(. )\\d $", "\\1", excel), ""), function(x) {
val <- match(rev(x), LETTERS)
sum(val * 26^(seq_along(val) - 1))
})
data.frame(excel = excel, r_row = r_rows, r_col = r_cols)
#> excel r_row r_col
#> 1 F3 3 6
#> 2 BG5 5 59
或者,如果您只想location在串列中替換(使用 Ritchie 的示例資料),您可以執行以下操作:
lapply(list_of_comments, function(l) {
excel <- l$location
r_row <- as.numeric(sub("^.*?(\\d )$", "\\1", excel))
r_col <- sapply(strsplit(sub("^(. )\\d $", "\\1", excel), ""), function(x) {
val <- match(rev(x), LETTERS)
sum(val * 26^(seq_along(val) - 1))
})
l$location <- c(row = r_row, col = r_col)
l
})
#> [[1]]
#> [[1]]$location
#> row col
#> 5 59
#>
#> [[1]]$comment
#> [1] "abc"
#>
#>
#> [[2]]
#> [[2]]$location
#> row col
#> 2 27
#>
#> [[2]]$comment
#> [1] "xyz"
由
如果您使用tidyxl-pacgage 閱讀 excel,則可以直接從 tne 行/列中匯出everyting
library(tidyxl)
cells <- xlsx_cells("./temp/xl_test.xlsx")
cells[!is.na(cells$comment), c(1:4,17)]
# A tibble: 1 x 5
# sheet address row col comment
# <chr> <chr> <int> <int> <chr>
# 1 Blad1 C2 2 3 "wim:\r\ncomment1"
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/456765.html
上一篇:excel從sheet1中的A列參考sheet2中的A列
下一篇:如何在電子表格中總結結果
