【问题标题】:How do I access the elements of my list without getting an error message如何在不收到错误消息的情况下访问列表中的元素
【发布时间】: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 以包含错误。

标签: r dplyr tidyverse


【解决方案1】:

您正在用kable 函数的输出(带有样式)替换数据框df。在将数据传输到 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()

【讨论】:

  • 我尝试了你的方法,但我仍然遇到同样的错误。我已经更新了 reprex,因此错误一目了然。
  • 你没有按照我说的去做。而且您的解决方案没有回答您的问题:“将所有列表元素连接成一个字符串”。
【解决方案2】:

解决这个问题比我想象的要容易得多 - 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 创建

【讨论】:

  • 不幸的是,这并没有回答问题,即:“[如何] 将所有列表元素连接成一个 single 字符串”。 unnest 函数创建 两个 字符串!
  • @Edward - 你是对的,但是一旦你解除了访问数据的琐碎事情。 .
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-11
  • 2013-08-17
  • 1970-01-01
  • 2023-04-05
相关资源
最近更新 更多