需要在 R 中使用 across() 同时创建多个新变量
Need to create multiple new variables simultaneously using across() in R
我有一个数据集,我需要为 inflation 调整多个变量。它看起来像这样。
year
price1
price2
price3
price4
2003
1.149
1.149
1.163
1.172
2004
1.169
1.164
1.184
1.18
2005
1.167
1.166
1.183
1.178
我需要把这些都放在一个固定的格式中(比如 2020 美元)。我可以使用 priceR
包中的 adjust_for_inflation
函数轻松完成此操作。但是,有 lot 个价格变量,所以我想自动创建它们。我一直在尝试使用 across
但它不起作用。这是我一直在尝试的。
library(tidyverse)
library(priceR)
#this is it done manually, which would take hours
df %>% mutate(adjusted_price1=adjust_for_inflation(price1,year,"US",to_date = 2020))
#here's my attempt to do it all at once
price.vars <- df %>% select(-year) %>% names()
dollars2020 <- function(x){
y <- adjust_for_inflation(x,year,"US",to_date = 2020)
}
df <- df %>%
mutate(across(price.vars, dollars2020,.names ="adjusted_{col}"))
据我所知,这应该是吐出一个新变量列表,其名称如 adjusted_price1
等等。但它不起作用。如果有人能提供任何帮助,我将不胜感激。
问题不在于您对 across
的使用,而是您的功能。首先,您将一个名为 year
的变量传递给 adjust_for_inflation
,但该变量并不存在。其次,你的函数没有 return 任何东西。如果将其更改为:
dollars2020 <- function(x){
adjust_for_inflation(x, 2022,"US",to_date = 2020)
}
您将获得:
df %>%
mutate(across(price.vars, dollars2020,.names ="adjusted_{col}"))
#> year price1 price2 price3 price4 adjusted_price1 adjusted_price2 adjusted_price3 adjusted_price4
#> 1 2003 1.149 1.149 1.163 1.172 1.134999 1.134999 1.148828 1.157719
#> 2 2004 1.169 1.164 1.184 1.180 1.154755 1.149816 1.169572 1.165621
#> 3 2005 1.167 1.166 1.183 1.178 1.152779 1.151792 1.168585 1.163645
只是命名部分不起作用吗?如果是,则将 {col} 更改为 {.col}
也许这对你有用。我将 adjust_for_inflation
直接传递到您的 dplyr
行,而不是自定义函数:
代码
library(dplyr)
library(priceR)
price.vars <- df %>% select(-year) %>% names()
df %>% mutate(across(price.vars, ~ adjust_for_inflation(.x, year, "US", to_date = 2020), .names = "adjusted_{col}"))
输出
# A tibble: 3 x 9
year price1 price2 price3 price4 adjusted_price1 adjusted_price2 adjusted_price3 adjusted_price4
<int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2003 1.15 1.15 1.16 1.17 1.62 1.62 1.64 1.65
2 2004 1.17 1.16 1.18 1.18 1.60 1.59 1.62 1.62
3 2005 1.17 1.17 1.18 1.18 1.55 1.55 1.57 1.56
数据
df <- tibble(fread("year price1 price2 price3 price4
2003 1.149 1.149 1.163 1.172
2004 1.169 1.164 1.184 1.18
2005 1.167 1.166 1.183 1.178"))
我有一个数据集,我需要为 inflation 调整多个变量。它看起来像这样。
year | price1 | price2 | price3 | price4 |
---|---|---|---|---|
2003 | 1.149 | 1.149 | 1.163 | 1.172 |
2004 | 1.169 | 1.164 | 1.184 | 1.18 |
2005 | 1.167 | 1.166 | 1.183 | 1.178 |
我需要把这些都放在一个固定的格式中(比如 2020 美元)。我可以使用 priceR
包中的 adjust_for_inflation
函数轻松完成此操作。但是,有 lot 个价格变量,所以我想自动创建它们。我一直在尝试使用 across
但它不起作用。这是我一直在尝试的。
library(tidyverse)
library(priceR)
#this is it done manually, which would take hours
df %>% mutate(adjusted_price1=adjust_for_inflation(price1,year,"US",to_date = 2020))
#here's my attempt to do it all at once
price.vars <- df %>% select(-year) %>% names()
dollars2020 <- function(x){
y <- adjust_for_inflation(x,year,"US",to_date = 2020)
}
df <- df %>%
mutate(across(price.vars, dollars2020,.names ="adjusted_{col}"))
据我所知,这应该是吐出一个新变量列表,其名称如 adjusted_price1
等等。但它不起作用。如果有人能提供任何帮助,我将不胜感激。
问题不在于您对 across
的使用,而是您的功能。首先,您将一个名为 year
的变量传递给 adjust_for_inflation
,但该变量并不存在。其次,你的函数没有 return 任何东西。如果将其更改为:
dollars2020 <- function(x){
adjust_for_inflation(x, 2022,"US",to_date = 2020)
}
您将获得:
df %>%
mutate(across(price.vars, dollars2020,.names ="adjusted_{col}"))
#> year price1 price2 price3 price4 adjusted_price1 adjusted_price2 adjusted_price3 adjusted_price4
#> 1 2003 1.149 1.149 1.163 1.172 1.134999 1.134999 1.148828 1.157719
#> 2 2004 1.169 1.164 1.184 1.180 1.154755 1.149816 1.169572 1.165621
#> 3 2005 1.167 1.166 1.183 1.178 1.152779 1.151792 1.168585 1.163645
只是命名部分不起作用吗?如果是,则将 {col} 更改为 {.col}
也许这对你有用。我将 adjust_for_inflation
直接传递到您的 dplyr
行,而不是自定义函数:
代码
library(dplyr)
library(priceR)
price.vars <- df %>% select(-year) %>% names()
df %>% mutate(across(price.vars, ~ adjust_for_inflation(.x, year, "US", to_date = 2020), .names = "adjusted_{col}"))
输出
# A tibble: 3 x 9
year price1 price2 price3 price4 adjusted_price1 adjusted_price2 adjusted_price3 adjusted_price4
<int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2003 1.15 1.15 1.16 1.17 1.62 1.62 1.64 1.65
2 2004 1.17 1.16 1.18 1.18 1.60 1.59 1.62 1.62
3 2005 1.17 1.17 1.18 1.18 1.55 1.55 1.57 1.56
数据
df <- tibble(fread("year price1 price2 price3 price4
2003 1.149 1.149 1.163 1.172
2004 1.169 1.164 1.184 1.18
2005 1.167 1.166 1.183 1.178"))