R 排除列时跨列突变的正确语法,第2部分

R 排除列时跨列突变的正确语法,第2部分,r,dplyr,mutate,across,R,Dplyr,Mutate,Across,我以为我已经找到了问题的答案,但是当我使用更大的数据集时,我得到了不同的结果。我怀疑差异是因为na.locf行的工作方式不同 基本上,我正在使用mutate\u at将代码转换为带有mutate(cross())的新语法 在下面的第一种情况下,数据填写正确,因为df_initial仍然按索引名称分组。在第二种情况下,我假设,因为我必须为整个的变异解组才能工作,所以我得到了不同的答案 因此,这里有另一个更大数据集的例子来说明这个问题 可复制示例: df_initial <- structu

我以为我已经找到了问题的答案,但是当我使用更大的数据集时,我得到了不同的结果。我怀疑差异是因为
na.locf
行的工作方式不同

基本上,我正在使用
mutate\u at
将代码转换为带有
mutate(cross())
的新语法

在下面的第一种情况下,数据填写正确,因为
df_initial
仍然按索引名称分组。在第二种情况下,我假设,因为我必须为整个
变异
解组才能工作,所以我得到了不同的答案

因此,这里有另一个更大数据集的例子来说明这个问题

可复制示例:

df_initial <- 
structure(list(Date = structure(c(18681, 18681, 18681, 18681, 
                                  18682, 18682, 18682, 18682, 18683, 18683, 18683, 18683, 18684, 
                                  18684, 18684, 18684, 18685, 18685, 18685, 18685, 18686, 18686, 
                                  18686, 18686), class = "Date"), index_name = c("INDU Index", 
                                                                                 "SPX Index", "TPX Index", "MEXBOL Index", "INDU Index", "SPX Index", 
                                                                                 "TPX Index", "MEXBOL Index", "INDU Index", "SPX Index", "TPX Index", 
                                                                                 "MEXBOL Index", "INDU Index", "SPX Index", "TPX Index", "MEXBOL Index", 
                                                                                 "INDU Index", "SPX Index", "TPX Index", "MEXBOL Index", "INDU Index", 
                                                                                 "SPX Index", "TPX Index", "MEXBOL Index"), index_level = c(31537.35, 
                                                                                                                                            3881.37, NA, 45268.33, 31961.86, 3925.43, 1903.07, 45151.38, 
                                                                                                                                            31402.01, 3829.34, 1926.23, 44310.27, 30932.37, 3811.15, 1864.49, 
                                                                                                                                            44592.91, NA, NA, NA, NA, NA, NA, NA, NA), totalReturn_daily = c(0.0497, 
                                                                                                                                                                                                             0.1277, 0, 0.7158, 1.3461, 1.1364, -1.8201, -0.1151, -1.7181, 
                                                                                                                                                                                                             -2.4339, 1.2411, -1.8629, -1.4628, -0.4636, -3.2052, 0.6379, 
                                                                                                                                                                                                             0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -24L), groups = structure(list(
                                                                                                                                                                                                               index_name = c("INDU Index", "MEXBOL Index", "SPX Index", 
                                                                                                                                                                                                                              "TPX Index"), .rows = structure(list(c(1L, 5L, 9L, 13L, 17L, 
                                                                                                                                                                                                                                                                     21L), c(4L, 8L, 12L, 16L, 20L, 24L), c(2L, 6L, 10L, 14L, 
                                                                                                                                                                                                                                                                                                            18L, 22L), c(3L, 7L, 11L, 15L, 19L, 23L)), ptype = integer(0), class = c("vctrs_list_of", 
                                                                                                                                                                                                                                                                                                                                                                                     "vctrs_vctr", "list"))), row.names = c(NA, -4L), class = c("tbl_df", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                "tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               "tbl_df", "tbl", "data.frame"))
df_初始值%
在(变量(-index\u name,-totalReturn\u daily)处进行变异,
~na.locf(,na.rm=FALSE))%>%
过滤器(索引\u名称==“TPX索引”)
#输出
日期索引\u名称索引\u级别总回报\u每日
2021-02-23 TPX指数NA 0
2021-02-24 TPX指数1903-1.82
3 2021-02-25 TPX指数1926。1.24
4 2021-02-26 TPX指数1864-3.21
52021-02-27 TPX指数1864。0
6 2021-02-28 TPX指数1864。0
#方法2:此处未收到预期输出
df_初始%>%
解组()%>%
变异(
.cols=-c(索引名称,总返回值,每天),
.fns=~na.locf(,na.rm=FALSE)
)) %>%
过滤器(索引\u名称==“TPX索引”)
#输出
日期索引\u名称索引\u级别总回报\u每日
1201-02-23 TPX指数3881。0
2021-02-24 TPX指数1903-1.82
3 2021-02-25 TPX指数1926。1.24
4 2021-02-26 TPX指数1864-3.21
5 2021-02-27 TPX指数44593。0
6 2021-02-28 TPX指数44593。0

谢谢

这两种方法给我的结果相似。你能试试下面的代码吗

library(zoo)
df_initial %>%
  group_by(index_name) %>% 
  mutate_at(vars(-index_name, -totalReturn_daily),
            ~ na.locf(., na.rm = FALSE)) %>% 
  dplyr::filter(index_name == "TPX Index") 


df_initial %>%
  group_by(index_name) %>% 
  mutate(across(
    .cols = -c(totalReturn_daily),
    .fns  = ~ na.locf(., na.rm = FALSE)
  )) %>%
  ungroup() %>% 
  dplyr::filter(index_name == "TPX Index")

的确如此。为什么
-index\u name
不需要在第二个版本中,但它确实需要在第一个版本中?在第一种方法中它也不需要在那里。我想我遗漏了一个地方,如果你在
group\u by
中有一个变量,那么它在
处被屏蔽,而在
处被
变异
?换句话说,我不需要指定从mutate命令中排除该变量,因为它是自动排除的?这可能会有帮助:太好了,感谢您的参考和解决方案!
library(zoo)
df_initial %>%
  group_by(index_name) %>% 
  mutate_at(vars(-index_name, -totalReturn_daily),
            ~ na.locf(., na.rm = FALSE)) %>% 
  dplyr::filter(index_name == "TPX Index") 


df_initial %>%
  group_by(index_name) %>% 
  mutate(across(
    .cols = -c(totalReturn_daily),
    .fns  = ~ na.locf(., na.rm = FALSE)
  )) %>%
  ungroup() %>% 
  dplyr::filter(index_name == "TPX Index")