通过R中的特定模式将文本转换为数据帧

时间:2017-11-25 12:35:05

标签: r

我有这样的文字:

text <- "Jeon Bo-ram (born March 22, 1986):She, better known mononymously as Boram, is a South Korean singer and actress. She is best known as a member of the South Korean girl group T-ara. Taylor Alison Swift (born December 13, 1989): She is an American singer-songwriter. One of the leading contemporary recording artists, she is known for narrative songs about her personal life, which have received widespread media coverage. Nickolas Gene Carter (born January 28, 1980): He is an American musician and actor. He is best known as a member of the pop group the Backstreet Boys. As of 2015, Carter has released three solo albums, Now or Never, I'm Taking Off and All American during breaks between Backstreet Boys schedules, and a collaboration with Jordan Knight titled Nick & Knight. He has made occasional television appearances and starred in his own reality shows, House of Carters and I (Heart) Nick Carter. He gained fame in the mid 1990s and early 2000s as a teen idol. He is also the older brother of singer Aaron Carter and the late Leslie Carter. Clyde Jackson Browne (born October 9, 1948): He is an American singer-songwriter, and musician who has sold over 18 million albums in the United States. Coming to prominence in the 1970s, Browne has written and recorded songs such as These Days, The Pretender, Running on Empty, Lawyers in Love, Doctor My Eyes, Take It Easy, and  For a Rocker. In 2004, he was inducted into the Rock and Roll Hall of Fame in Cleveland, Ohio, and given an honorary doctorate of music by Occidental College in Los Angeles, California."

任何有关此类内容的建议:

name                      born                         detail
Jeon Bo-ram               born March 22, 1986          She, better known mononymously as Boram, is a South Korean singer and actress. She is best known as a member of the South Korean girl group T-ara. Taylor Alison Swift (born December 13, 1989): She is an American singer-songwriter. One of the leading contemporary recording artists, she is known for narrative songs about her personal life, which have received widespread media coverage.
Taylor Alison Swift       born December 13, 1989       She is an American singer-songwriter. One of the leading contemporary recording artists, she is known for narrative songs about her personal life, which have received widespread media coverage.
Nickolas Gene Carter      born January 28, 1980        He is an American musician and actor. He is best known as a member of the pop group the Backstreet Boys. As of 2015, Carter has released three solo albums, Now or Never, I'm Taking Off and All American during breaks between Backstreet Boys schedules, and a collaboration with Jordan Knight titled Nick & Knight. He has made occasional television appearances and starred in his own reality shows, House of Carters and I (Heart) Nick Carter. He gained fame in the mid 1990s and early 2000s as a teen idol. He is also the older brother of singer Aaron Carter and the late Leslie Carter.
Clyde Jackson Browne      born October 9, 1948         He is an American singer-songwriter, and musician who has sold over 18 million albums in the United States. Coming to prominence in the 1970s, Browne has written and recorded songs such as These Days, The Pretender, Running on Empty, Lawyers in Love, Doctor My Eyes, Take It Easy, and For a Rocker. In 2004, he was inducted into the Rock and Roll Hall of Fame in Cleveland, Ohio, and given an honorary doctorate of music by Occidental College in Los Angeles, California.

我试着这样做但无法解决问题。

cbind(do.call(rbind, strsplit(text, ":")), sub(".*[ ]", "", text))

2 个答案:

答案 0 :(得分:2)

正则表达式是这里的方法,只要你保证模式不会改变你制作一个更具适应性的正则表达式。

library(stringi)
library(purrr)

text <- "Jeon Bo-ram (born March 22, 1986):She, better known mononymously as Boram, is a South Korean singer and actress. She is best known as a member of the South Korean girl group T-ara. Taylor Alison Swift (born December 13, 1989): She is an American singer-songwriter. One of the leading contemporary recording artists, she is known for narrative songs about her personal life, which have received widespread media coverage. Nickolas Gene Carter (born January 28, 1980): He is an American musician and actor. He is best known as a member of the pop group the Backstreet Boys. As of 2015, Carter has released three solo albums, Now or Never, I'm Taking Off and All American during breaks between Backstreet Boys schedules, and a collaboration with Jordan Knight titled Nick & Knight. He has made occasional television appearances and starred in his own reality shows, House of Carters and I (Heart) Nick Carter. He gained fame in the mid 1990s and early 2000s as a teen idol. He is also the older brother of singer Aaron Carter and the late Leslie Carter. Clyde Jackson Browne (born October 9, 1948): He is an American singer-songwriter, and musician who has sold over 18 million albums in the United States. Coming to prominence in the 1970s, Browne has written and recorded songs such as These Days, The Pretender, Running on Empty, Lawyers in Love, Doctor My Eyes, Take It Easy, and  For a Rocker. In 2004, he was inducted into the Rock and Roll Hall of Fame in Cleveland, Ohio, and given an honorary doctorate of music by Occidental College in Los Angeles, California."

让我们将blob按摩成更容易管理的东西:

stri_replace_all_regex(
  text,
  "([[:alpha:][:space:]\\-]+ \\(born [[:alpha:]]+ [[:digit:]]+, [[:digit:]]+\\):)",
  "\n$1\n"
) %>% 
  stri_split_lines() %>% 
  flatten_chr() %>% 
  discard(`==`, "") %>% 
  stri_trim_both() -> lines

lines现在看起来像这样:

lines
## [1] "Jeon Bo-ram (born March 22, 1986):"    
## [2] "She, better known mononymously as Boram, is a South Korean singer and actress. She is best known as a member of the South Korean girl group T-ara."
## [3] "Taylor Alison Swift (born December 13, 1989):"
## [4] "She is an American singer-songwriter. One of the leading contemporary recording artists, she is known for narrative songs about her personal life, which have received widespread media coverage."
## [5] "Nickolas Gene Carter (born January 28, 1980):"
## [6] "He is an American musician and actor. He is best known as a member of the pop group the Backstreet Boys. As of 2015, Carter has released three solo albums, Now or Never, I'm Taking Off and All American during breaks between Backstreet Boys schedules, and a collaboration with Jordan Knight titled Nick & Knight. He has made occasional television appearances and starred in his own reality shows, House of Carters and I (Heart) Nick Carter. He gained fame in the mid 1990s and early 2000s as a teen idol. He is also the older brother of singer Aaron Carter and the late Leslie Carter."
## [7] "Clyde Jackson Browne (born October 9, 1948):"
## [8] "He is an American singer-songwriter, and musician who has sold over 18 million albums in the United States. Coming to prominence in the 1970s, Browne has written and recorded songs such as These Days, The Pretender, Running on Empty, Lawyers in Love, Doctor My Eyes, Take It Easy, and  For a Rocker. In 2004, he was inducted into the Rock and Roll Hall of Fame in Cleveland, Ohio, and given an honorary doctorate of music by Occidental College in Los Angeles, California."      

正则表达式查找名称和日期模式并将其拆分。这种“过度分裂”,但discard()负责空白行。

现在我们有了名称/ dob的行对和描述,并且可以使用“by 2”索引迭代它们:

starts <- seq(1, length(lines), 2) 
ends <- starts + 1

map2_df(starts, ends, ~{

  stri_split_fixed(lines[.x], "(")[[1]] %>% 
    stri_replace_all_fixed("):", "") %>% 
    stri_replace_all_fixed("born ", "") -> name_dob

  data_frame(
    name = name_dob[1],
    born = name_dob[2],
    detail = lines[.y]
  )

}) -> xdf

内部区块的第一部分分离&amp;清理名称/ dob。后者生成一个数据框行,purrr的{​​{1}}将整个事物转换为数据框:

map2_df()

如果您正在处理可能导致数千行的大型文本博客,那么使用glimpse(xdf) ## Observations: 4 ## Variables: 3 ## $ name <chr> "Jeon Bo-ram ", "Taylor Alison Swift ", "Nickolas Gene Carter ",... ## $ born <chr> "March 22, 1986", "December 13, 1989", "January 28, 1980", "Octo... ## $ detail <chr> "She, better known mononymously as Boram, is a South Korean sing... vs list()(内部块的最后一部分)将更快并且消耗更少的临时内存

现在,可能有可能创建一个单一的正则表达式来查找每个三元组并提取它们,但是如果他们倾向于创建那些可能难以理解的野兽,我会让其他人展示他们的l33t正则表达式技能。 / p>

答案 1 :(得分:1)

使用正则表达式

A <- unlist(strsplit(text, "[(](?=b)|[)]:|(?<=\\.) (?=[^.]+?\\(b)", perl=TRUE))
B <- sapply(A, function(i) trimws(i, "both"))
as.data.frame(matrix(B, ncol=3, byrow=TRUE))