我有一个问题,目前我正在尝试拆分excel文件中的单元格,如果该行以数字开头(以及两位数的加号)并且数字后跟句号,我可以拆分每一行“.”,即由该函数中定义的正则表达式定义。但是,当实际拆分发生时,输出是基于其他列的重复(循环)。
# Load libraries
library('tidyverse')
library('readxl')
library('openxlsx')
# Set functions
do_split = function(x, pattern = "\\d+\\.\\s{1}"){
if( is_tibble(x) ){ x = pull(x) }
num_bullets = x %>% str_extract_all("\\d+\\. ") %>% unlist
x %>% str_split(pattern) %>% unlist %>% .[.!=""] %>% str_c(num_bullets,.) %>% list %>% return
}
# Read data
df = read_excel(path = '~/Desktop/master.xlsx')
# Wrangle data
o = df %>%
mutate(Result = Result %>% do_split, Steps = Steps %>% do_split) %>%
unnest(Result, Steps)
# Output file
write.xlsx(x = o, file = “out.xlsx”)