我制作了一個函式,它可以跨列進行變異,并從每個列中創建新的命名列。新列放在資料框的右側,而我希望它們與每個原始列相鄰。我正在尋找一種解決方案,該解決方案將推廣到可能使用此函式的任何資料幀,撰寫一個 select 陳述句來重新排序列對于我的用例來說不夠自動化。
test_data <- data.frame(data_col_1 = c(1,2,3),
data_col_2 = c(1,2,3),
data_col_3 = c(1,2,3),
another_column = c("a","b","c"))
perc_funct <- function(df, columns, numerator){
p_f <- function(x, numerator){
(x/numerator)*100
}
j <- df %>%
mutate( across({{columns}},
.fns = list(perc = ~p_f(.x, numerator)),
.names = "{col}_{fn}"))# need to figure out a way to get the columns ordered
return(j)
}
test_data %>% perc_funct(columns = starts_with("data"), numerator = 1)
輸出當前將所有新列放在右側。
“data_col_1” “data_col_2” “data_col_3” “another_column” “data_col_1_perc” “data_col_2_perc” “data_col_3_perc”
我想要的輸出將每個新列放在每個舊列的右側。“data_col_1” “data_col_1_perc” “data_col_2” “data_col_2_perc” “data_col_3” “data_col_3_perc” “another_column”
uj5u.com熱心網友回復:
我通常在之后對列進行排序select(sort(names(.))):
library(dplyr)
test_data %>%
perc_funct(columns = starts_with("data"), numerator = 1) %>%
select(sort(names(.)))
#> data_col_1 data_col_1_perc data_col_2 data_col_2_perc data_col_3
#> 1 1 100 1 100 1
#> 2 2 200 2 200 2
#> 3 3 300 3 300 3
#> data_col_3_perc
#> 1 100
#> 2 200
#> 3 300
由reprex 包于 2022-04-01 創建(v2.0.1)
如果我想在同一位置保留其他列怎么辦?
這只是將我的解決方案與其他select陳述句或 dplyr 動詞嵌套在一起的問題。作為中間步驟,您可能必須保存帶有未排序列的資料框。
示例 1
這是一個包含其他三個列的示例,我希望一些列在前,一些列在最后,而另一些列在任何地方,但要保持在一起。
library(dplyr)
df <-
test_data %>%
mutate(first_col = 1, other_columns = 100, last_col = 999) %>%
perc_funct(columns = starts_with("data"), numerator = 1)
# Unsorted:
df %>% names()
#> [1] "data_col_1" "data_col_2" "data_col_3" "first_col"
#> [5] "other_columns" "last_col" "data_col_1_perc" "data_col_2_perc"
#> [9] "data_col_3_perc"
# Sorted:
df %>%
select(
first_col,
df %>% select(starts_with("data")) %>% names() %>% sort(),
everything(),
last_col
)
#> first_col data_col_1 data_col_1_perc data_col_2 data_col_2_perc data_col_3
#> 1 1 1 100 1 100 1
#> 2 1 2 200 2 200 2
#> 3 1 3 300 3 300 3
#> data_col_3_perc other_columns last_col
#> 1 100 100 999
#> 2 200 100 999
#> 3 300 100 999
由reprex 包于 2022-04-01 創建(v2.0.1)
示例 2
還有一種使用方法col_bind():
如果您只想將新列放在最后,但與創建它們的列一起排序,您還可以執行以下操作:
library(dplyr)
df %>%
select(
-starts_with("data")
) %>% bind_cols(
df %>%
select(
df %>% select(starts_with("data")) %>% names() %>% sort()
)
)
#> first_col other_columns last_col data_col_1 data_col_1_perc data_col_2
#> 1 1 100 999 1 100 1
#> 2 1 100 999 2 200 2
#> 3 1 100 999 3 300 3
#> data_col_2_perc data_col_3 data_col_3_perc
#> 1 100 1 100
#> 2 200 2 200
#> 3 300 3 300
uj5u.com熱心網友回復:
使用 dplyr(從 1.0.0 版開始)移動列的推薦方法是使用relocate(). relocate()支持 tidyselect 語意,但重要的是僅對選定的列起作用,而將所有其他列保留在原位。在您的情況下,您可以在以 . 開頭的列grep()上。sort()data
test_data <- data.frame(column_1 = 1:3,
data_col_1 = c(1,2,3),
data_col_2 = c(1,2,3),
data_col_3 = c(1,2,3),
another_column = c("a","b","c"))
test_data %>%
perc_funct(columns = starts_with("data"), numerator = 1) %>%
relocate(sort(grep("^data", names(.), value = TRUE)), .before = data_col_1)
column_1 data_col_1 data_col_1_perc data_col_2 data_col_2_perc data_col_3 data_col_3_perc another_column
1 1 1 100 1 100 1 100 a
2 2 2 200 2 200 2 200 b
3 3 3 300 3 300 3 300 c
( .beforeor .after) 引數指定將列重定位的位置,在這種情況下,您可以將它們放在 之前data_col_1。
uj5u.com熱心網友回復:
另一種可能性是使用contains()原始資料框中的列順序
test_data <- data.frame(column_1 = 1:3,
data_col_1 = c(1,2,3),
data_col_2 = c(1,2,3),
data_col_3 = c(1,2,3),
another_column = c("a","b","c"))
test_data %>% perc_funct(columns = starts_with("data"), numerator = 1) %>%
select(contains(test_data %>% colnames()))
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/454424.html
上一篇:當表大小可變時,如何使用dplyr指定要變異的列索引?
下一篇:使用R聚合和匯總資料集資訊
