我有一个通过str_extract函数创建的列表。该列表由字符串组成,我想将所有列表元素连接成一个字符串。不幸的是,当我尝试访问元素时,出现以下错误消息:
column must be of length (number of rows or one) not 2
我尝试了几种不同的方法,包括[[1]]和purrr :: pluck(),但始终遇到相同的错误。
以下是带有示例代码的reprex:
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创建
答案 0 :(得分:1)
您正在用df
函数(带有样式)的输出替换数据帧kable
。在将数据传输到kable之前先停止:
df2 <- df %>%
mutate(htext = str_extract_all(text,(str_c('(?<=^|\\.)[^.]*\\b',name,'\\b[^.]*\\.?')))) %>%
select(-text) %>%
mutate(type = typeof(htext))
df2 %>% kable() %>%
kable_styling()
现在您可以访问df2
的元素。
df2$htext
#[[1]]
#[1] " Passing the 300 Metres, SUPREME PROFIT lay in and proved reluctant to shift to the outside of DOUBLE DRAGON."
#[2] " DOUBLE DRAGON and PLAIN BLUE BANNER were sent for sampling."
现在,您可以执行任何您喜欢的操作,包括将它们全部串联成一个字符串。
paste(unlist(df2$htext), collapse=" : ") # or whatever you prefer.
因此您的命令将是:
df <- data.frame(name=name, text=myStr) # R 4.0.0
df %>%
mutate(htext = str_extract_all(text,(str_c('(?<=^|\\.)[^.]*\\b',name,'\\b[^.]*\\.?')))) %>%
select(-text) %>%
mutate(single_string = paste(unlist(htext), collapse=" : ")) %>%
kable() %>%
kable_styling()
答案 1 :(得分:0)
解决这个问题的方法比我想象的要容易得多-unnest()可以解决问题。
library(tidyverse)
library(reprex)
library(kableExtra)
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) %>%
unnest(htext) %>%
kable() %>%
kable_styling()
由reprex package(v0.3.0)于2020-05-11创建