簡體   English   中英

更改data.frame中的長變量名

[英]Change long variable names in data.frame

我在下面的data.frame df中擁有長的變量名。

每個名稱的第一部分是主要類別(岩石,土壤,土地利用),第二部分通常由幾個名稱組成,即級別(例如,對於岩石,兩個級別是sandstone mudstone basalt chert limestonesandstone conglomerate coquina tephra sandstone mudstone basalt chert limestonesandstone mudstone basalt chert limestonesandstone conglomerate coquina tephra )。

> df
# A tibble: 5 x 2
  `rock_sandstone conglomerate coquina tephra` `rock_sandstone mudstone basalt chert limestone`
                                         <dbl>                                            <dbl>
1                                     0.000000                                        18.774037
2                                    41.968310                                        30.276509
3                                    32.804031                                         0.000000
4                                     8.669436                                         3.092062
5                                    32.937377                                        19.894776

我想通過僅使用每個單詞的第一個字母來縮短變量名稱,如下所示。 我可以使用dplyr::rename做到這一點。 但是,我有97個變量,並且我想對具有不同變量名的20個data.frames執行相同的操作。 我想知道是否有更快的方法。

library(dplyr)
df <- df %>% rename("r_sccat"  = 'rock_sandstone conglomerate coquina tephra',
                    "r_smbcl" =  "rock_sandstone mudstone basalt chert limestone")

> df
# A tibble: 5 x 2
    r_sccat   r_smbcl
      <dbl>     <dbl>
1  0.000000 18.774037
2 41.968310 30.276509
3 32.804031  0.000000
4  8.669436  3.092062
5 32.937377 19.894776

數據

> dput(df)
structure(list(`rock_sandstone conglomerate coquina tephra` = c(0, 
41.9683095321332, 32.8040311360418, 8.66943642122745, 32.9373770476129
), `rock_sandstone mudstone basalt chert limestone` = c(18.7740373237074, 
30.2765089609693, 0, 3.09206176664796, 19.8947759845006)), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("rock_sandstone conglomerate coquina tephra", 
"rock_sandstone mudstone basalt chert limestone"))

有點難看,但是abbreviate和一些正則表達式替換將使您到達那里:

names(df) <- sub("^(.)", "\\1_", abbreviate(gsub("_", " ", names(df))))
df

## A tibble: 5 × 2
#     r_scct   r_smbcl
#      <dbl>     <dbl>
#1  0.000000 18.774037
#2 41.968310 30.276509
#3 32.804031  0.000000
#4  8.669436  3.092062
#5 32.937377 19.894776

我不熟悉縮寫,但是可以通過一些正則表達式直接實現相同的縮寫:

names( df ) <- gsub( ' ', "", gsub( "([a-z])([a-z]+)", "\\1", names( df ) ) )

使用magrittr可以使語法更簡潔:

require( magrittr )
names( df ) %<>%
    gsub( "([a-z])([a-z]+)", "\\1", . ) %>%
    gsub( " ", "", . )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM