Search code examples
rparsingcpu-wordrecycle

Parsing strings with punctuation, in uneven rows from a large list in r


My data contains a library of words I am trying to import. The difference with this questions is I am separating words and punctuation. My original file has about 1,000,000 rows. I figured out how to separate the punctuation. However, I figured out that my rows are now uneven and they are recycled to match the longest length of the row. I am trying to stop the recycling and instead add NA values for the recycling. I have attached my data I am importing = df. And my expected output as = output.

#--------Upload 1st dataset and edit-------#
library("stringr")
sent1<-c("How did Quebec? 1 2 3")
sent2<-c("Why does valve = .245")
sent3<-c("How do I use a period (.) comma [,] and hyphen {-} to columns?")
df <- data.frame(text = c(sent1,sent2,sent3))

#--Parse the punctation and the words from df
df<-do.call(cbind, lapply(gsub("([[:punct:]])", " \\1 ", 
        df$text), function(x) scan(text = x, what = "", quiet = TRUE)))

This is the result I get now...

> do.call(cbind, lapply(gsub("([[:punct:]])", " \\1 ", 
+         df$text), function(x) scan(text = x, what = "", quiet = TRUE)))

      [,1]     [,2]    [,3]     
 [1,] "How"    "Why"   "How"    
 [2,] "did"    "does"  "do"     
 [3,] "Quebec" "valve" "I"      
 [4,] "?"      "="     "use"    
 [5,] "1"      "."     "a"      
 [6,] "2"      "245"   "period" 
 [7,] "3"      "Why"   "("      
 [8,] "How"    "does"  "."      
 [9,] "did"    "valve" ")"      
[10,] "Quebec" "="     "comma"  
[11,] "?"      "."     "["      
[12,] "1"      "245"   ","      
[13,] "2"      "Why"   "]"      
[14,] "3"      "does"  "and"    
[15,] "How"    "valve" "hyphen" 
[16,] "did"    "="     "{"      
[17,] "Quebec" "."     "-"      
[18,] "?"      "245"   "}"      
[19,] "1"      "Why"   "to"     
[20,] "2"      "does"  "columns"
[21,] "3"      "valve" "?"  

This is my output I want...

#-------------output-------------#
words1<-c("How", "did" ,"Quebec"," ? ","1", "2" ,"3",NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA)
words2<-c('Why', "does", "valve"," = ",".","245" ,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA)
words3<-c("How" ,"do", "I", "use", "a", "period", '(',".",')', "comma" ,'[',",","]" ,"and" ,"hyphen" ,"{","-",'}' ,"to" ,"columns",'?')
output<-data.frame(words1,words2,words3)

Output

> data.frame(words1,words2,words3)
   words1 words2  words3
1     How    Why     How
2     did   does      do
3  Quebec  valve       I
4      ?      =      use
5       1      .       a
6       2    245  period
7       3   <NA>       (
8    <NA>   <NA>       .
9    <NA>   <NA>       )
10   <NA>   <NA>   comma
11   <NA>   <NA>       [
12   <NA>   <NA>       ,
13   <NA>   <NA>       ]
14   <NA>   <NA>     and
15   <NA>   <NA>  hyphen
16   <NA>   <NA>       {
17   <NA>   <NA>       -
18   <NA>   <NA>       }
19   <NA>   <NA>      to
20   <NA>   <NA> columns
21   <NA>   <NA>       ?

Solution

  • You could use cbind.fill from rowr package with additional argument of fill = NA

    library(rowr)
    do.call(cbind.fill, c(lapply(gsub("([[:punct:]])", " \\1 ", 
            df$text), function(x) scan(text = x, what = "", quiet = TRUE)), fill = NA))
    
    #   object object  object
    #1     How    Why     How
    #2     did   does      do
    #3  Quebec  valve       I
    #4       ?      =     use
    #5       1      .       a
    #6       2    245  period
    #7       3   <NA>       (
    #8    <NA>   <NA>       .
    #9    <NA>   <NA>       )
    #10   <NA>   <NA>   comma
    #11   <NA>   <NA>       [
    #12   <NA>   <NA>       ,
    #13   <NA>   <NA>       ]
    #14   <NA>   <NA>     and
    #15   <NA>   <NA>  hyphen
    #16   <NA>   <NA>       {
    #17   <NA>   <NA>       -
    #18   <NA>   <NA>       }
    #19   <NA>   <NA>      to
    #20   <NA>   <NA> columns
    #21   <NA>   <NA>       ?
    

    Or a base R option using sapply

    list_df <- lapply(gsub("([[:punct:]])", " \\1 ", 
                   df$text), function(x) scan(text = x, what = "", quiet = TRUE))
    
    sapply(list_df, '[', 1:max(lengths(list_df)))
    
    #       [,1]     [,2]    [,3]     
    # [1,] "How"    "Why"   "How"    
    # [2,] "did"    "does"  "do"     
    # [3,] "Quebec" "valve" "I"      
    # [4,] "?"      "="     "use"    
    # [5,] "1"      "."     "a"      
    # [6,] "2"      "245"   "period" 
    # [7,] "3"      NA      "("      
    # [8,] NA       NA      "."      
    # [9,] NA       NA      ")"      
    #[10,] NA       NA      "comma"  
    #[11,] NA       NA      "["      
    #......