Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何在代码中提供单双精度向量而不是双精度向量_R_Tidyverse_Purrr - Fatal编程技术网

R 如何在代码中提供单双精度向量而不是双精度向量

R 如何在代码中提供单双精度向量而不是双精度向量,r,tidyverse,purrr,R,Tidyverse,Purrr,我有一些代码利用pmap_dbl来计算一些分割。我创建了一个未加权版本和一个加权版本。未加权版本按预期运行并生成所需的输出 加权版本利用我的getRwt函数(最近权重),并尝试将计算出的权重应用于分割计算。此版本引发以下错误: Error: Problem with `mutate()` input `splt_1a`. x Result 1 must be a single double, not a double vector of length 4 i Input `splt_1a` is

我有一些代码利用pmap_dbl来计算一些分割。我创建了一个未加权版本和一个加权版本。未加权版本按预期运行并生成所需的输出

加权版本利用我的getRwt函数(最近权重),并尝试将计算出的权重应用于分割计算。此版本引发以下错误:

Error: Problem with `mutate()` input `splt_1a`. x Result 1 must be a single double, not a double vector of length 4 i Input `splt_1a` is `pmap_dbl(...)`.
我相信这个错误告诉我,我提供的不是一个单独的比赛日期,而是一个比赛日期列。不幸的是,我不知道如何提供个人日期。函数所关注的比赛日期是当前行的比赛日期

下面是我的tibble、函数和代码的一个reprex

library(tidyverse)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

df <- tibble(name=c("Bill","Bill","Bill","Bill"), n=c(1,2,3,4), trk=c("GP","GP","SA","GP"), TE=c(2,4,0,1), race_date=c("11/1/2020", "12/31/2020","2/28/2021","4/7/2021"))

df
#> # A tibble: 4 x 5
#>   name      n trk      TE race_date 
#>   <chr> <dbl> <chr> <dbl> <chr>     
#> 1 Bill      1 GP        2 11/1/2020 
#> 2 Bill      2 GP        4 12/31/2020
#> 3 Bill      3 SA        0 2/28/2021 
#> 4 Bill      4 GP        1 4/7/2021

getRwt <- function(a, b){
  interval <- interval(a, b)
  d <- round(time_length(interval, "day"))
  t <- if_else(d < 366,d/7, d/5) /290
  rwt <- exp(1)^-t
  return(rwt)
}

# Unweighted Version

df %>% 
  mutate(splt_1 = pmap_dbl(list(trk, n),  ~ sum(if_else(trk == ..1 & ..2 > n, TE, as.numeric(0))))) 
#> # A tibble: 4 x 6
#>   name      n trk      TE race_date  splt_1
#>   <chr> <dbl> <chr> <dbl> <chr>       <dbl>
#> 1 Bill      1 GP        2 11/1/2020       0
#> 2 Bill      2 GP        4 12/31/2020      2
#> 3 Bill      3 SA        0 2/28/2021       0
#> 4 Bill      4 GP        1 4/7/2021        6

# Recency Weighted Version

df %>% 
  mutate(splt_1a = pmap_dbl(list(trk, n, race_date),  ~ sum(if_else(trk == ..1 & ..2 > n, TE, as.numeric(0))) * getRwt(race_date, ..3)))

#> Error: Problem with `mutate()` input `splt_1a`.
#> x Result 1 must be a single double, not a double vector of length 4
#> i Input `splt_1a` is `pmap_dbl(...)`.
库(tidyverse)
图书馆(lubridate)
#> 
#>附加包装:“lubridate”
#>以下对象已从“package:base”屏蔽:
#> 
#>日期、相交、设置差异、联合
df#A tible:4 x 5
#>姓名与比赛日期
#>            
#>1 2020年11月1日第1号法案总包2
#>2 2020年12月31日第2号法案总包4
#>3法案3 SA 0 2/28/2021
#>2021年7月4日第4号工程量清单总成1
getRwt
#>1工程量清单1总成2 2020年11月1日0
#>2 2020年12月31日第2号法案总包4 2
#>3法案3 SA 0 2/28/2021 0
#>4 2021年7月4日第4号工程量清单GP 1 6
#最近加权版本
df%>%
变异(splt_1a=pmap_dbl(list(trk,n,race_date))~sum(if_else(trk==…1&…2>n,TE,as.numeric(0))*getRwt(race_date,…3)))
#>错误:“mutate()”输入“splt_1a”有问题。
#>x结果1必须是单双精度向量,而不是长度为4的双精度向量
#>我输入的'splt_1a'是'pmap_dbl(…)`。

由reprex软件包(v0.3.0)于2021年4月14日创建。

您必须将
race\u date
的列类型更改为
date

library(dplyr)
library(tidyr)
library(purrr)
library(lubridate)

df %>% 
  mutate(race_date = mdy(race_date), 
         splt_1a = pmap_dbl(list(trk, n, race_date),  ~ sum(if_else(trk == ..1 & ..2 > n, TE * getRwt(race_date, ..3), as.numeric(0))))) %>%
  unnest_wider(splt_1a) 

# A tibble: 4 x 6
  name      n trk      TE race_date   ...1
  <chr> <dbl> <chr> <dbl> <date>     <dbl>
1 Bill      1 GP        2 2020-11-01  0   
2 Bill      2 GP        4 2020-12-31  1.94
3 Bill      3 SA        0 2021-02-28  0   
4 Bill      4 GP        1 2021-04-07  5.66

库(dplyr)
图书馆(tidyr)
图书馆(purrr)
图书馆(lubridate)
df%>%
变异(种族日期=mdy(种族日期),
splt_1a=pmap_dbl(列表(trk,n,race_date),~sum(如果其他(trk==…1&…2>n,TE*getRwt(race_date,…3),如.numeric(0‘‘’))%>%
最宽(splt_1a)
#一个tibble:4x6
姓名与比赛日期…1
1工程量清单1总成2 2020-11-01 0
2工程量清单2 GP 4 2020-12-31 1.94
3工程量清单3 SA 0 2021-02-28 0
4工程量清单4总成1 2021-04-07 5.66

我使用了
pmap\u dfc
而不是
pmap\u dbl
,因为它不会产生一个双精度。输出正确吗?我更新了我的代码。是的,可能就是这样。你是对的!完成!