【发布时间】:2020-05-11 11:50:26
【问题描述】:
我有一个通过 str_extract 函数创建的列表。该列表由字符串组成,我喜欢将所有列表元素连接成一个字符串。不幸的是,当我尝试访问元素时,我收到以下错误消息:
column must be of length (number of rows or one) not 2
我尝试了几种不同的方法,包括 [[1]] 和 purrr::pluck() ,但总是遇到同样的错误。
这是一个带有示例代码的代表:
library(tidyverse)
myStr <- 'Approaching the 1300 Metres, TAI PO FORTUNE blundered when being shifted in behind AMAZING GIFT. TAI PO FORTUNE then got its head up when racing keenly passing the 1000 Metres. Near the 800 Metres, COOL PAL was left racing wide and without cover. Passing the 300 Metres, SUPREME PROFIT lay in and proved reluctant to shift to the outside of DOUBLE DRAGON. SUPREME PROFIT continued to hang in under pressure and over the concluding stages raced tight outside GOLDWEAVER. Because of this, SUPREME PROFIT was not able to be properly tested over the concluding stages. DOUBLE DRAGON and PLAIN BLUE BANNER were sent for sampling.'
name <- "DOUBLE DRAGON"
df <- as_tibble(data.frame(name=c(name),text=c(myStr),stringsAsFactors = FALSE))
df <- df %>%
mutate(htext = str_extract_all(text,(str_c('(?<=^|\\.)[^.]*\\b',name,'\\b[^.]*\\.?')))) %>%
select(-text) %>%
mutate(htext1 = htext[[1]]) %>%
mutate(htext2 = htext[[2]]) %>%
print(head(df,n=10))
#> Error: Column `htext1` must be length 1 (the number of rows), not 2
由reprex package (v0.3.0) 于 2020-05-11 创建
【问题讨论】:
-
我提供的代码没有任何错误。
-
@Edward 我没有尝试在提供的代码中访问该列表。也许我应该有。
-
是的,当然!您应该编辑您的问题并包含此信息。
-
如果我尝试
paste0(df),我不会收到任何错误消息,您到底想做什么? -
@我已经更新了 reprex 以包含错误。