首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >代码逐行运行得很好,但在rmarkdown中作为一个整体运行时会失败

代码逐行运行得很好,但在rmarkdown中作为一个整体运行时会失败
EN

Stack Overflow用户
提问于 2021-02-05 06:47:58
回答 1查看 20关注 0票数 1

当我只运行这一行代码时,结果与预期一致。当我运行块时,突变在第三行停止。我如何解决这个问题,我觉得这是我以前在相同的代码中没有遇到过的新东西。

示例数据:

代码语言:javascript
复制
> dput(head(out))
structure(list(SectionCut = c("S-1", "S-1", "S-1", "S-1", "S-2", 
"S-2"), OutputCase = c("LL-1", "LL-2", "LL-3", "LL-4", "LL-1", 
"LL-2"), V2 = c(81.782, 119.251, 119.924, 96.282, 72.503, 109.595
), M3 = c("-29.292000000000002", "-32.661999999999999", "-30.904", 
"-23.632999999999999", "29.619", "32.994"), id = c("./100-12-S01.xlsx", 
"./100-12-S01.xlsx", "./100-12-S01.xlsx", "./100-12-S01.xlsx", 
"./100-12-S01.xlsx", "./100-12-S01.xlsx")), row.names = c(NA, 
-6L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), groups = structure(list(
SectionCut = c("S-1", "S-1", "S-1", "S-1", "S-2", "S-2"), 
OutputCase = c("LL-1", "LL-2", "LL-3", "LL-4", "LL-1", "LL-2"
), id = c("./100-12-S01.xlsx", "./100-12-S01.xlsx", "./100-12-S01.xlsx", 
"./100-12-S01.xlsx", "./100-12-S01.xlsx", "./100-12-S01.xlsx"
), .rows = list(1L, 2L, 3L, 4L, 5L, 6L)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))

> dput(head(Beamline_Shear))
structure(list(VLL = c(159.512186, 154.3336, 149.4451613, 167.0207595, 
161.2269091, 156.4116505)), row.names = c("84-9", "84-12", "84-15", 
"92-9", "92-12", "92-15"), class = "data.frame")

我正在尝试运行的代码:

代码语言:javascript
复制
Shear <- out[,-4] %>% mutate(N_l = str_extract(OutputCase,"\\d+"), 
                          UG = str_extract(id,"\\d+"), a = str_extract(id,"-\\d+"),
                          S = str_extract(a,"\\d+"), Sections = paste0(UG,"-",S),
                          Sample = str_remove_all(id, "./\\d+-\\d+-|.xlsx")) %>%
  left_join(Beamline_Shear %>% rownames_to_column("Sections"), by = "Sections") %>% 
  select(-OutputCase,-id,-Sections,-a)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-05 07:01:54

数据中有一些组属性,它们应该正常工作,但如果我们在不同的环境中运行,可能会出现问题。此外,mutate步骤和join步骤实际上不需要任何分组属性,因为它们是非常简单的、矢量化的行式操作。

代码语言:javascript
复制
library(dplyr)
out %>% 
    select(-4) %>%
    ungroup  %>% # // removes group attributes
    mutate(N_l = str_extract(OutputCase,"\\d+"), 
                           UG = str_extract(id,"\\d+"), a = str_extract(id,"-\\d+"),
                           S = str_extract(a,"\\d+"), Sections = paste0(UG,"-",S),
                           Sample = str_remove_all(id, "./\\d+-\\d+-|.xlsx")) %>% left_join(Beamline_Shear %>% rownames_to_column("Sections"), by = "Sections")
# A tibble: 6 x 11
#  SectionCut OutputCase    V2 id                N_l   UG    a     S     Sections Sample   VLL
#  <chr>      <chr>      <dbl> <chr>             <chr> <chr> <chr> <chr> <chr>    <chr>  <dbl>
#1 S-1        LL-1        81.8 ./100-12-S01.xlsx 1     100   -12   12    100-12   S01       NA
#2 S-1        LL-2       119.  ./100-12-S01.xlsx 2     100   -12   12    100-12   S01       NA
#3 S-1        LL-3       120.  ./100-12-S01.xlsx 3     100   -12   12    100-12   S01       NA
#4 S-1        LL-4        96.3 ./100-12-S01.xlsx 4     100   -12   12    100-12   S01       NA
#5 S-2        LL-1        72.5 ./100-12-S01.xlsx 1     100   -12   12    100-12   S01       NA
#6 S-2        LL-2       110.  ./100-12-S01.xlsx 2     100   -12   12    100-12   S01       NA
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66054998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档