假設我有一組列,在我的示例中命名為, Intercept, 1,以及一組系數,通過命名。23c0c3
xs<-seq(.1,1,.1)
X <- cbind(Intercept=1, "1"=xs, "2"=xs^2, "3"=xs^3)
coefs <- c(c0=10, c1=2, c2=.5, c3=-1)
我想將 X 的每一列乘以相應的系數。
sweep(
X, # x: the data array
2, # MARGIN: 2, to sweep across rows
coefs, # STATS: just the array of coefficients
`*`) # FUN: the function to use is multiplication
這給了我想要的。
但是,如果我將資料作為 tibble ( tidyX <- as_tibble(X)),那么這樣做的整潔方法是什么?
tidyX %>% ... ?
這看起來很簡單,我想它dplyr::rowwise()可能涉及到 ,但我沒有看到這樣做的慣用方式。
uj5u.com熱心網友回復:
啊,就在我終于發帖的時候,我找到了答案。
tidyX %>%
rowwise() %>%
mutate(across() * coefs)
我仍然覺得這種語法不直觀,但這正是我正在尋找的。
uj5u.com熱心網友回復:
如果列名相同,我們可以回圈across'tidyX' 的列,并使用列名 ( cur_column()) 來提取 'coefs' 的對應列。但是,這里的列名不同,所以用于match獲取列索引,[[從 'coefs' 中提取 ( ) 列/元素(如果它是命名向量)并相乘
library(dplyr)
tidyX %>%
mutate(across(everything(),
~ .x * coefs[[match(cur_column(), names(tidyX))]]))
或者更簡單的選擇是map2(from purrr) 回圈遍歷兩個資料集的相應列并相乘。如果我們希望輸出為 a tibble/data.frame,請使用_dfc
library(purrr)
map2_df(tidyX, coefs, `*`)
# A tibble: 10 × 4
Intercept `1` `2` `3`
<dbl> <dbl> <dbl> <dbl>
1 10 0.2 0.005 -0.001
2 10 0.4 0.02 -0.008
3 10 0.6 0.045 -0.027
4 10 0.8 0.08 -0.064
5 10 1 0.125 -0.125
6 10 1.2 0.18 -0.216
7 10 1.4 0.245 -0.343
8 10 1.6 0.32 -0.512
9 10 1.8 0.405 -0.729
10 10 2 0.5 -1
uj5u.com熱心網友回復:
這是使用旋轉的另一種方法:
library(dplyr)
library(tidyr)
coefs <- c(c0=10, c1=2, c2=.5, c3=-1)
X %>%
as_tibble() %>%
mutate(row = row_number()) %>%
pivot_longer(
-row
) %>%
mutate(value = value*coefs) %>%
pivot_wider(
names_from = name,
values_from = value
) %>%
select(-row)
Intercept `1` `2` `3`
<dbl> <dbl> <dbl> <dbl>
1 10 0.2 0.005 -0.001
2 10 0.4 0.02 -0.008
3 10 0.6 0.045 -0.027
4 10 0.8 0.08 -0.064
5 10 1 0.125 -0.125
6 10 1.2 0.18 -0.216
7 10 1.4 0.245 -0.343
8 10 1.6 0.32 -0.512
9 10 1.8 0.405 -0.729
10 10 2 0.5 -1
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/419359.html
標籤:
上一篇:根據因子級別重新格式化標簽/保留ggplot2::facet_wrap()中多因子構面的順序
下一篇:在多列的字串中查找最佳值
