嗨,我有點新,所以我不確定我是否做得對,但我環顧四周,找不到適用于我的代碼的代碼或建議。
我有一個如下所示的資料框 mainDF:
| 人 | ABG | 九月 | CLC | XSP | 應用程式 | 星期三 | 谷胱甘肽 |
|---|---|---|---|---|---|---|---|
| SP-1 | 2.1 | 3.0 | 1.3 | 1.8 | 1.4 | 2.5 | 1.4 |
| SP-2 | 2.5 | 2.1 | 2.0 | 1.9 | 1.2 | 1.2 | 2.1 |
| SP-3 | 2.3 | 3.1 | 2.5 | 1.5 | 1.1 | 2.6 | 2.1 |
我有另一個資料框 TranslateDF,其中包含縮寫列名的轉換資訊。我想在這里用真實姓名替換縮寫名稱:
請注意,翻譯資料框可能包含無關資訊,或者可能缺少列的資訊,因此如果 mainDF 沒有獲得完整命名,則將其從資料中洗掉。
| 縮寫 | 全稱 |
|---|---|
| ABG | 所有燒烤架 |
| 九月 | 搖動鰻魚皮 |
| CLC | 冷腰蛋糕 |
| XSP | 木琴矛品脫 |
| 應用程式 | 蘋果鍋餅 |
| 哼 | 大廳聯合肉 |
| LPL | 出借豬里脊肉 |
理想情況下,我會得到新的結果資料框:
| 人 | 所有燒烤架 | 搖動鰻魚皮 | 冷腰蛋糕 | 木琴矛品脫 | 蘋果鍋餅 |
|---|---|---|---|---|---|
| SP-1 | 2.1 | 3.0 | 1.3 | 1.8 | 1.4 |
| SP-2 | 2.5 | 2.1 | 2.0 | 1.9 | 1.2 |
| SP-3 | 2.3 | 3.1 | 2.5 | 1.5 | 1.1 |
我將不勝感激任何幫助,謝謝!
uj5u.com熱心網友回復:
您可以傳遞一個命名向量,select()該向量將在一個步驟中重命名和選擇。包裝any_of()確保如果主資料框中不存在任何列,它不會失敗:
library(dplyr)
df1 %>%
select(Person, any_of(setNames(df2$Abbreviated, df2$Full_Naming)))
# A tibble: 3 x 6
Person `All barbecue grill` `shake eel peel` `cold loin cake` `xylophone spear pint` `apple pot pie`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 SP-1 2.1 3 1.3 1.8 1.4
2 SP-2 2.5 2.1 2 1.9 1.2
3 SP-3 2.3 3.1 2.5 1.5 1.1
資料:
df1 <- structure(list(Person = c("SP-1", "SP-2", "SP-3"), ABG = c(2.1,
2.5, 2.3), SEP = c(3, 2.1, 3.1), CLC = c(1.3, 2, 2.5), XSP = c(1.8,
1.9, 1.5), APP = c(1.4, 1.2, 1.1), WED = c(2.5, 1.2, 2.6), GSH = c(1.4,
2.1, 2.1)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -3L), spec = structure(list(cols = list(
Person = structure(list(), class = c("collector_character",
"collector")), ABG = structure(list(), class = c("collector_double",
"collector")), SEP = structure(list(), class = c("collector_double",
"collector")), CLC = structure(list(), class = c("collector_double",
"collector")), XSP = structure(list(), class = c("collector_double",
"collector")), APP = structure(list(), class = c("collector_double",
"collector")), WED = structure(list(), class = c("collector_double",
"collector")), GSH = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))
df2 <- structure(list(Abbreviated = c("ABG", "SEP", "CLC", "XSP", "APP",
"HUM", "LPL"), Full_Naming = c("All barbecue grill", "shake eel peel",
"cold loin cake", "xylophone spear pint", "apple pot pie", "hall united meat",
"lending porkloin")), class = "data.frame", row.names = c(NA,
-7L))
uj5u.com熱心網友回復:
這個怎么樣:
mainDF <- structure(list(Person = c("SP-1", "SP-2", "SP-3"), ABG = c(2.1,
2.5, 2.3), SEP = c(3, 2.1, 3.1), CLC = c(1.3, 2, 2.5), XSP = c(1.8,
1.9, 1.5), APP = c(1.4, 1.2, 1.1), WED = c(2.5, 1.2, 2.6), GSH = c(1.4,
2.1, 2.1)), row.names = c(NA, 3L), class = "data.frame")
translateDF <- structure(list(Abbreviated = c("ABG", "SEP", "CLC", "XSP", "APP",
"HUM", "LPL"), `Full Naming` = c("All barbecue grill", "shake eel peel",
"cold loin cake", "xylophone spear pint", "apple pot pie", "hall united meat",
"lending porkloin")), row.names = c(NA, 7L), class = "data.frame")
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tidyr)
mainDF %>%
pivot_longer(-Person,
names_to="Abbreviated",
values_to = "vals") %>%
left_join(translateDF) %>%
select(-Abbreviated) %>%
na.omit() %>%
pivot_wider(names_from=`Full Naming`, values_from="vals")
#> Joining, by = "Abbreviated"
#> # A tibble: 3 × 6
#> Person `All barbecue grill` `shake eel peel` `cold loin cake` `xylophone spe…`
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 SP-1 2.1 3 1.3 1.8
#> 2 SP-2 2.5 2.1 2 1.9
#> 3 SP-3 2.3 3.1 2.5 1.5
#> # … with 1 more variable: `apple pot pie` <dbl>
由reprex 包于 2022-04-24 創建(v2.0.1)
uj5u.com熱心網友回復:
library(tidyverse)
mainDF %>%
rename_with(~str_replace_all(., set_names(TranslateDF[, 2], TranslateDF[, 1]))) %>%
select(Person, which(!(names(.) %in% names(mainDF))))
Person All barbecue grill shake eel peel cold loin cake xylophone spear pint apple pot pie
1 SP-1 2.1 3.0 1.3 1.8 1.4
2 SP-2 2.5 2.1 2.0 1.9 1.2
3 SP-3 2.3 3.1 2.5 1.5 1.1
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/463624.html
