camelarms
camelarms

Reputation: 110

Splitting a string in R, different split argument elements

I imported some data with no column names, so now I have just over a million rows, and 1 column (instead of 5 columns).

Each row is formatted like this:

x <- "2012-10-19T16:59:01-07:00 192.101.136.140 <190>Oct 19 2012 23:59:01: %FWSM-6-305011: Built dynamic tcp translation from Inside:10.2.45.62/56455 to outside:192.101.136.224/9874"

strsplit( x , split = c(" ", " ", "%", " "))

and got

[[1]]
 [1] "2012-10-19T16:59:01-07:00"    "192.101.136.140"             
 [3] "<190>Oct"                     "19"                          
 [5] "2012"                         "23:59:01:"                   
 [7] "%FWSM-6-305011:"              "Built"                       
 [9] "dynamic"                      "tcp"                         
[11] "translation"                  "from"                        
[13] "Inside:10.2.45.62/56455"      "to"                          
[15] "outside:192.101.136.224/9874"

I know that it has to do with recycling the split argument but I can't seem to figure how to get it how I want:

    [[1]]
     [1] "2012-10-19T16:59:01-07:00"    "192.101.136.140"             
     [3] "<190>Oct 19 2012 23:59:01     "%FWSM-6-305011
     [5] Built dynamic tcp translation from Inside:10.2.45.62/56455 to outside:192.101.136.224/9874"

Each row has a different message as the fifth element, but after the 4th element I just want to keep the rest of the string together.

Any help would be appreciated.

Upvotes: 2

Views: 453

Answers (3)

nograpes
nograpes

Reputation: 18323

Here is a function that I think works in the way that you thought strsplit functioned:

split.seq<-function(x,delimiters) {
  break.point<-regexpr(delimiters[1], x)
  first<-mapply(substring,x,1,break.point-1,USE.NAMES=FALSE)
  second<-mapply(substring,x,break.point+1,nchar(x),USE.NAMES=FALSE)
  if (length(delimiters)==1)  return(lapply(1:length(first),function(x) c(first[x],second[x])))
  else mapply(function(x,y) c(x,y),first, split.seq(second, delimiters[-1]) ,USE.NAMES=FALSE, SIMPLIFY=FALSE)
}

split.seq(x,delimiters)

A test:

x<-rep(x,2)             
delimiters=c(" ", " ", "%", " ")
split.seq(x,delimiters)

[[1]]
[1] "2012-10-19T16:59:01-07:00"                                                                 
[2] "192.101.136.140"                                                                           
[3] "<190>Oct 19 2012 23:59:01: "                                                               
[4] "FWSM-6-305011:"                                                                            
[5] "Built dynamic tcp translation from Inside:10.2.45.62/56455 to outside:192.101.136.224/9874"

[[2]]
[1] "2012-10-19T16:59:01-07:00"                                                                 
[2] "192.101.136.140"                                                                           
[3] "<190>Oct 19 2012 23:59:01: "                                                               
[4] "FWSM-6-305011:"                                                                            
[5] "Built dynamic tcp translation from Inside:10.2.45.62/56455 to outside:192.101.136.224/9874"

Upvotes: 0

agstudy
agstudy

Reputation: 121568

I think here you don't need to use strsplit. I use read.table to read the lines using text argument. Then you aggregate columns using paste. Since you have a lot of rows, it is better to do the column aggregation within a data.table.

dt <- read.table(text=x)
library(data.table)
DT <- as.data.table(dt)
DT[ , c('V3','V8') := list(paste(V3,V4,V5),
      V8=paste(V8,V9,V10,V11,V12,V13,V14,V15))]
DT[,paste0('V',c(1:3,6:7,8)),with=FALSE]

                        V1              V2               V3        V6              V7
1: 2012-10-19T16:59:01-07:00 192.101.136.140 <190>Oct 19 2012 23:59:01: %FWSM-6-305011:
                                                                                           V8
1: Built dynamic tcp translation from Inside:10.2.45.62/56455 to outside:192.101.136.224/9874

Upvotes: 0

Se&#241;or O
Se&#241;or O

Reputation: 17412

You can use paste with the collapse argument to combine every element starting with the fifth element.

A <- strsplit( x = "2012-10-19T16:59:01-07:00 192.101.136.140 <190>Oct 19 2012 23:59:01: %FWSM-6-305011: Built dynamic tcp translation from Inside:10.2.45.62/56455 to outside:192.101.136.224/9874", split = c(" ", " ", "%", " "))

c(A[[1]][1:4], paste(A[[1]][5:length(A[[1]])], collapse=" "))

As @DWin points out, split = c(" ", " ", "%", " ") is not used in order - in other words it's identical to split = c(" ", "%")

Upvotes: 2

Related Questions