0

I have some data that looks like this:

   dat <- c("Sales","Jim","Halpert","","",
            "Reception","Pam","Beasley","","",
            "Not.Manager","Dwight","Schrute","Bears","Beets","BattlestarGalactica","","",
            "Manager","Michael","Scott","","")

Each "chunk" of data is consecutive with some blanks in between. I want to transform the data into a list of lists that looks like this:

iwant <- c(
           c("Sales","Jim","Halpert"),
           c("Reception","Pam","Beasley"),
           c("Not.Manager","Dwight","Schrute","Bears","Beets","BattlestarGalactica"),
           c("Manager","Michael","Scott")
           )

Suggestions? I am using rvest and stringi. I do not want to add more packages.

1
  • Both dat and iwant are one single vector.
    – Ronak Shah
    Aug 21, 2020 at 0:13

2 Answers 2

1

You can use rle, split with lapply :

lapply(split(dat, with(rle(dat != ''), 
             rep(cumsum(values), lengths))), function(x) x[x!= ''])

#$`1`
#[1] "Sales"   "Jim"     "Halpert"

#$`2`
#[1] "Reception" "Pam"       "Beasley"  

#$`3`
#[1] "Not.Manager"         "Dwight"    "Schrute"     "Bears"   "Beets"            
#[6] "BattlestarGalactica"

#$`4`
#[1] "Manager" "Michael" "Scott"  

rle part creates group to split on :

with(rle(dat != ''), rep(cumsum(values), lengths))
#[1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4

After split we use lapply to remove any empty elements from each list.

1
  • Your answer is robust to spaces and other non alpha-numeric characters in my strings. That is something I didn't know I was looking for!
    – bstrain
    Aug 21, 2020 at 13:27
1

I would suggest next approach. You will end up with a dataframe with the variables in a similar format to that you want:

#Split chains
L1 <- strsplit(paste0(dat,collapse = " "),split = "  ")
#Split vectors from each chain
L2 <- lapply(L1[[1]],function(x) strsplit(trimws(x),split = " "))
#Format
L2 <- lapply(L2,as.data.frame)
#Remove zero dim data
L2[which(lapply(L2,nrow)==0)]<-NULL
#Format names
L2 <- lapply(L2,function(x) {names(x)<-'v';return(x)})
#Transform to dataframe
D1 <- as.data.frame(do.call(cbind,L2))
#Rename
names(D1) <- paste0('V',1:dim(D1)[2])
#Remove recycled values
D1 <- as.data.frame(apply(D1,2,function(x) {x[duplicated(x)]<-NA;return(x)}))

Output:

       V1        V2                  V3      V4
1   Sales Reception         Not.Manager Manager
2     Jim       Pam              Dwight Michael
3 Halpert   Beasley             Schrute   Scott
4    <NA>      <NA>               Bears    <NA>
5    <NA>      <NA>               Beets    <NA>
6    <NA>      <NA> BattlestarGalactica    <NA>
1
  • 1
    I get > D1 <- as.data.frame(do.call(cbind,L2)) Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 9, 8, 7, 4, 12' My actual data contains spaces, which I think is the cause. My fault for not including spaces in the strings in the original question. Ronak's answer works on my actual data so I have accepted his, but yours works wonderfully on the example data.
    – bstrain
    Aug 21, 2020 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.