bstrain
bstrain

Reputation: 278

Create New Lists Based on List Structure Pattern

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.

Upvotes: 0

Views: 86

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 389325

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.

Upvotes: 1

Duck
Duck

Reputation: 39613

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>

Upvotes: 1

Related Questions