Question

Have a list of character() vectors of different length. How you can transform it into a data.frame efficiently (with some Xapply function). Also a additional ID column is required.

INPUT

# sentences are like:
sent1 <- "ab"
sent2 <- "bc"
sent3 <- "cd"
sent4 <- "de"

# sections
sec1 <- c(sent1, sent3)
sec2 <- c(sent4, sent3, sent2)
sec3 <- c(sent3)
sec4 <- c(sent2, sent1)

# the whole list
text <- list(sec1, sec2, sec3, sec4)

my try is NOT WORKING

text2.df <- lapply(text, function(i)
                         lapply(text[[i]], function(j) 
                                           data.frame(ID=paste(sprintf("%02d", i), sprintf("%03d", j), sep = ""), # creates the requred sentence IDs (works properly)
                                                               Sentence=text[[i]][j])))

Requred OUTPUT

> text2.df 
     ID Sentences
1 01001        ab
2 01002        cd
3 02001        de
4 02002        cd
5 02003        bc
6 03001        cd
7 04001        bc
8 04002        ab
Was it helpful?

Solution

I don't see a lot of difference with your previous question except the last rbind. You should carefully read the answers.

ll <- lapply(seq_along(text),function(i)
  data.frame(ID = paste(sprintf("%02d", i), 
                   sprintf("%03d", seq_along(text[[i]])), 
                   sep = ""),
             sent=text[[i]]))

do.call(rbind,ll) 
     ID sent
1 01001   ab
2 01002   cd
3 02001   de
4 02002   cd
5 02003   bc
6 03001   cd
7 04001   bc
8 04002   ab
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top