Home > Blockchain >  Split a vector element in R
Split a vector element in R

Time:06-15

i have vector in this format with just one element:

t1
     [,1]                                                                                                                                                                                                                  
[1,] "<meta','name','description','content','Compre','ingressos','para','FEIRA','DIGITAL','EXPO','SP','2022','em','São','Paulo','dia','14','de','julho.','Confira','os','melhores','eventos','de','2022','na','Sympla!>\n"

and i need to split this into a vector with many elements, which are separated by comma, the purpose is to create a transactional dataframe to use with arules.

Could you help me? Thanks

CodePudding user response:

I think it is matrix 1X1 so try

strsplit(t1[1] , ",")

CodePudding user response:

It seems you have a single string in a 1x1 matrix. If you want to extract the words into a vector, with the quotes and angle brackets removed, you can do:

gsub('<|>|\n|\\!', "", strsplit(c(t1), "','")[[1]])
#>  [1] "meta"        "name"        "description" "content"     "Compre"     
#>  [6] "ingressos"   "para"        "FEIRA"       "DIGITAL"     "EXPO"       
#> [11] "SP"          "2022"        "em"          "São"        "Paulo"      
#> [16] "dia"         "14"          "de"          "julho."      "Confira"    
#> [21] "os"          "melhores"    "eventos"     "de"          "2022"       
#> [26] "na"          "Sympla"

Created on 2022-06-14 by the reprex package (v2.0.1)

  • Related