[英]R dplyr: Use the function as a string in one column on the next column
I want to apply a function, which name is stored in a column as a string, on a value in another column, using dplyr. 我想使用dplyr将一个函数应用于另一列中的值,该函数将名称作为字符串存储在列中。 I have tried several things using
mutate_
and a .dots
argument, but I am stuck now. 我已经使用
mutate_
和.dots
参数尝试了几个方法,但我现在被卡住了。
library(lubridate)
library(dplyr)
df <- data.frame(date=as.POSIXct('2017/01/01 12:34') + 1:10*123456,
fun=rep(c('minute','hour','day','month','year'),2))
input: 输入:
> df
date fun
1 2017-01-02 22:51:36 minute
2 2017-01-04 09:09:12 hour
3 2017-01-05 19:26:48 day
4 2017-01-07 05:44:24 month
5 2017-01-08 16:02:00 year
6 2017-01-10 02:19:36 minute
7 2017-01-11 12:37:12 hour
8 2017-01-12 22:54:48 day
9 2017-01-14 09:12:24 month
10 2017-01-15 19:30:00 year
output: 输出:
date fun res
1 2017-01-02 22:51:36 minute 51
2 2017-01-04 09:09:12 hour 9
3 2017-01-05 19:26:48 day 5
4 2017-01-07 05:44:24 month 1
5 2017-01-08 16:02:00 year 2017
6 2017-01-10 02:19:36 minute 19
7 2017-01-11 12:37:12 hour 12
8 2017-01-12 22:54:48 day 12
9 2017-01-14 09:12:24 month 1
10 2017-01-15 19:30:00 year 2017
One way , I could think of is using creating a lookup table and then getting the correct output format using match
我想到的一种方法是使用创建查找表,然后使用
match
获得正确的输出格式
x <- c("minute", "hour", "day", "month", "year")
y <- c("%M", "%H", "%d", "%m", "%Y")
format(df$date, format = y[match(df$fun, x)])
#[1] "51" "09" "05" "01" "2017" "19" "12" "12" "01" "2017"
Although, this gives a warning message but still the output is correct. 虽然,这会发出警告信息,但输出仍然正确。
If we need this in a dplyr
chain 如果我们需要在
dplyr
链中
library(dplyr)
df %>%
mutate(res = format(date, format = y[match(df$fun, x)]))
# date fun res
#1 2017-01-02 22:51:36 minute 51
#2 2017-01-04 09:09:12 hour 09
#3 2017-01-05 19:26:48 day 05
#4 2017-01-07 05:44:24 month 01
#5 2017-01-08 16:02:00 year 2017
#6 2017-01-10 02:19:36 minute 19
#7 2017-01-11 12:37:12 hour 12
#8 2017-01-12 22:54:48 day 12
#9 2017-01-14 09:12:24 month 01
#10 2017-01-15 19:30:00 year 2017
We can use mapply
我们可以使用
mapply
df$res <- mapply(function(x,y) get(x)(y), as.character(df$fun), df$date)
df$res
#[1] 51 9 5 1 2017 19 12 12 1 2017
Another option is data.table
另一种选择是
data.table
library(data.table)
setDT(df)[, res := as.integer(get(as.character(fun))(date)), 1:nrow(df)]
df
# date fun res
#1: 2017-01-02 22:51:36 minute 51
#2: 2017-01-04 09:09:12 hour 9
#3: 2017-01-05 19:26:48 day 5
#4: 2017-01-07 05:44:24 month 1
#5: 2017-01-08 16:02:00 year 2017
#6: 2017-01-10 02:19:36 minute 19
#7: 2017-01-11 12:37:12 hour 12
#8: 2017-01-12 22:54:48 day 12
#9: 2017-01-14 09:12:24 month 1
#10: 2017-01-15 19:30:00 year 2017
NOTE: Without making any additional effort in creating look up tables 注意:无需在创建查找表时进行任何额外的工作
You can try that with do.call
but you have to use rowwise
: 您可以使用
do.call
尝试,但必须使用rowwise
:
library("dplyr")
library("lubridate")
df <- data.frame(
date = as.POSIXct('2017/01/01 12:34') + 1:10*123456,
fun = rep(c('minute','hour','day','month','year'),2),
stringsAsFactors = FALSE
)
df %>% rowwise() %>% mutate(res = as.character(do.call(fun, list(date))))
To go full tidyverse here, we can use purrr's invoke_map()
function. 要在这里完全整理,我们可以使用purrr的
invoke_map()
函数。 It takes a list of functions and a list of lists of parameter values to use for each function. 它需要一个函数列表和一个用于每个函数的参数值列表。 It's like a vectorized
do.call()
. 它就像一个矢量化的
do.call()
。
The lubridate functions in df$fun
expect an argument x
, so we need to create a list of lists with each date stored as an element named x
. df$fun
的lubridate函数期望参数x
,因此我们需要创建一个列表列表,每个日期都存储为名为x
的元素。 We can create a list-column of data-frames by copying the date column and using nest()
. 我们可以通过复制日期列并使用
nest()
来创建数据框的列表列。
df2 <- df %>%
mutate(x = date) %>%
tidyr::nest(x, .key = "params")
df2
#> # A tibble: 10 × 3
#> date fun params
#> <dttm> <chr> <list>
#> 1 2017-01-02 22:51:36 minute <tibble [1 × 1]>
#> 2 2017-01-04 09:09:12 hour <tibble [1 × 1]>
#> 3 2017-01-05 19:26:48 day <tibble [1 × 1]>
#> 4 2017-01-07 05:44:24 month <tibble [1 × 1]>
#> 5 2017-01-08 16:02:00 year <tibble [1 × 1]>
#> 6 2017-01-10 02:19:36 minute <tibble [1 × 1]>
#> 7 2017-01-11 12:37:12 hour <tibble [1 × 1]>
#> 8 2017-01-12 22:54:48 day <tibble [1 × 1]>
#> 9 2017-01-14 09:12:24 month <tibble [1 × 1]>
#> 10 2017-01-15 19:30:00 year <tibble [1 × 1]>
Each element in the column params
is a data-frame with a column x
. 列
params
中的每个元素都是一个带有列x
的数据框。 This is our list of lists. 这是我们的清单清单。
df2$params[1]
#> [[1]]
#> # A tibble: 1 × 1
#> x
#> <dttm>
#> 1 2017-01-02 22:51:36
With our two lists, we can use invoke_map()
and get a list of results. 使用我们的两个列表,我们可以使用
invoke_map()
并获取结果列表。
str(purrr::invoke_map(df2$fun, df2$params))
#> List of 10
#> $ : int 51
#> $ : int 9
#> $ : int 5
#> $ : num 1
#> $ : num 2017
#> $ : int 19
#> $ : int 12
#> $ : int 12
#> $ : num 1
#> $ : num 2017
But because we know that these functions return just one numeric value each, we can get the results in a nice vector with invoke_map_dbl()
. 但是因为我们知道这些函数每个只返回一个数值,所以我们可以使用
invoke_map_dbl()
在一个很好的向量中得到结果。
df2 %>%
mutate(res = purrr::invoke_map_dbl(fun, params)) %>%
select(-params)
#> # A tibble: 10 × 3
#> date fun res
#> <dttm> <chr> <dbl>
#> 1 2017-01-02 22:51:36 minute 51
#> 2 2017-01-04 09:09:12 hour 9
#> 3 2017-01-05 19:26:48 day 5
#> 4 2017-01-07 05:44:24 month 1
#> 5 2017-01-08 16:02:00 year 2017
#> 6 2017-01-10 02:19:36 minute 19
#> 7 2017-01-11 12:37:12 hour 12
#> 8 2017-01-12 22:54:48 day 12
#> 9 2017-01-14 09:12:24 month 1
#> 10 2017-01-15 19:30:00 year 2017
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.