R语言 将数据框拆分为特定字符串上的多个像元

aemubtdh  于 2023-02-10  发布在  其他
关注(0)|答案(2)|浏览(132)

我在R中有一个 Dataframe ,基本上是这样的,一个文本主体,其中有一个line brake字符串(\r\n):

df <- data.frame (text  = c("non consectetur a erat nam at lectus urna duis convallis convallis tellus id interdum velit laoreet id donec ultrices tincidunt arcu non sodales neque sodales ut etiam sit amet nisl purus in mollis nunc sed id semper risus in hendrerit gravida rutrum quisque non tellus orci ac auctor augue mauris augue neque gravida in fermentum et sollicitudin ac orci phasellus egestas \r\n tellus rutrum tellus pellentesque eu tincidunt tortor aliquam nulla facilisi cras fermentum odio eu feugiat pretium nibh ipsum consequat nisl vel pretium lectus quam id leo in vitae turpis massa sed elementum tempus egestas sed sed risus pretium quam vulputate dignissim suspendisse in est ante in nibh mauris cursus mattis molestie a iaculis \r\n at erat pellentesque adipiscing commodo elit at imperdiet dui accumsan sit amet nulla facilisi morbi tempus iaculis urna id volutpat lacus laoreet non curabitur gravida arcu ac \r\n tortor dignissim convallis aenean et tortor at risus viverra adipiscing at in tellus integer feugiat scelerisque varius morbi enim nunc faucibus a pellentesque sit amet porttitor eget dolor morbi non arcu "))

每次出现分页符字符(\r\n)时,如何将此字符串拆分为多个 Dataframe 实体?

iyfjxgzm

iyfjxgzm1#

R base**strsplit()**函数可用于根据您提到的分页符字符(“\r\n”)将文本数据拆分为单独的字符串。生成的对象是一个列表,您可以将该列表绑定到数据框中:

df_list <- strsplit(df$text, "\r\n")
df_split <- data.frame(text = unlist(df_list))
omqzjyyz

omqzjyyz2#

我们可以使用separate_longer_delim

library(tidyr)
separate_longer_delim(df, text, delim = "\r\n")

相关问题