我有一個資料框df。我可以為 5 個不同的變數生成這個資料框 5 次。假設變數名稱是:
Apple # apple_df
Mango # mango_df
Banana # banana_df
Potato # potato_df
Tomato # tomato_df
每次生成資料框時,其中一個列名都非常大,例如:
Apple - Growth Level Judgement # Column name for apple_df
Mango - Growth Level Judgement # Column name for mango_df
Banana - Growth Level Judgement # Column name for banana_df
Potato - Growth Level Judgement # Column name for potato_df
Tomato - Growth Level Judgement # Column name for tomato_df
我想將上面的列名稱更改為Growth每個檔案中的單詞。
有沒有辦法通過使用一個公共代碼行(單獨)在所有資料幀中有效地做到這一點?
我可以在每個檔案中單獨使用完整名稱,但想知道我們是否可以有一個通用的解決方案:
# For Apple data frame
# Update column name
setnames(apple_df,
old = c('Apple - Growth Level Judgement'),
new = c('Growth'))
如果我使用以下基于正則運算式的解決方案,它只會替換所有資料幀中通用的字串名稱部分。不幸的是,不是全名。
gsub(x = names(apple_df),
pattern = "Growth Level Judgement$", replacement = "Growth")
相關文章:
以下帖子是相關的,但它去除了字串的已知部分Remove part of column name。就我而言,我想根據在多個資料集中保持相同的部分字串來檢測列的出現。但是一旦在列名中檢測到字串,我想更改整個列名。以下帖子也可能相關但不符合我的需求 r 洗掉某些字符后的部分列名或根據模式匹配重命名列名 R
對此的任何建議將不勝感激。謝謝!
uj5u.com熱心網友回復:
將資料框放入串列中并使用lapply/map更改每個資料框的名稱。list2env將這些更改從串列傳輸到單個資料幀。
library(dplyr)
library(purrr)
list_df <- lst(Apple, Mango, Banana, Potato, Tomato)
list_df <- map(list_df,
~.x %>% rename_with(~'Growth', matches('Growth Level Judgement')))
list2env(list_df, .GlobalEnv)
要在單個資料幀上運行它,您可以這樣做 -
Apple %>% rename_with(~'Growth', matches('Growth Level Judgement')))
或者在基礎 R -
names(Apple)[grep('Growth Level Judgement', names(Apple))] <- 'Growth'
uj5u.com熱心網友回復:
使用endsWith自base R
names(Apple)[endsWith(names(Apple), 'Growth Level Judgement')] <- 'Growth'
根據檔案?endsWith,它可能會更快
startsWith() 相當于但比它快得多
substring(x, 1, nchar(prefix)) == prefix
或者
grepl("^", x)
uj5u.com熱心網友回復:
另一種解決方案可能是:
Apple %>%
rename_with(~'Growth', ends_with('Growth Level Judgement'))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/336625.html
