Reputation: 3538
I have list that was created via a str_extract function. The list is comprised of strings and I like to concatenate all the list elements into a single string. Unfortunately when I try to access the element(s), I'm getting the following error message:
column must be of length (number of rows or one) not 2
I've tried a couple different approches including [[1]] and purrr::pluck() , but keep getting the same error.
Here is a reprex with sample code:
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
Created on 2020-05-11 by the reprex package (v0.3.0)
Upvotes: 0
Views: 65
Reputation: 18823
You are replacing the data frame, df
, with the output from the kable
function (with styling). Stop before you pipe the data to kable:
df2 <- df %>%
mutate(htext = str_extract_all(text,(str_c('(?<=^|\\.)[^.]*\\b',name,'\\b[^.]*\\.?')))) %>%
select(-text) %>%
mutate(type = typeof(htext))
df2 %>% kable() %>%
kable_styling()
Now you can access the elements of 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."
Now you can do whatever you like, including concatenating them all into a single string.
paste(unlist(df2$htext), collapse=" : ") # or whatever you prefer.
So your commands would be:
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()
Upvotes: 1
Reputation: 3538
The solution to this was much easier than I thought - unnest() does the trick.
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()
Created on 2020-05-11 by the reprex package (v0.3.0)
Upvotes: 0