0
votes

I have a list as below (2 elements provided) with length in the thousands.

[[1]]
[1] "few/JJ"        "enough/JJ"     "particular/JJ"

[[2]]
[1] "signifcant/JJ" "last/JJ"       "individual/JJ" "effective/JJ"

I'd like to convert it to a data frame with 1 column in the following format where the data types are characters:

 few enough particular

 significant last individual effective

I tried a few combinations of do.call, unlist and lapply, but can't seem to get the right format.

any help would be appreciated!

2
that gives me 7 rows for the list above. I want to return two rows, one for each list element shown above - Josh
So you don't want the /JJ part? - akrun
@Josh S It seems like you want it as strings "few enough particular" because of 1 column expected result. - akrun
I was hoping to also strip out the "/JJ" and just wanted the differing lengths to be ignored...i.e., a space between each element. thanks! - Josh
unlist(lapply(lst, function(x) paste(gsub("\\/.*", "", x), collapse=" "))) - akrun

2 Answers

0
votes

Where x is your list, you could use vapply since you know that the result will be a single-length character vector. This can sometimes be faster and safer since R knows beforehand the structure of the resulting vector.

data.frame(colname = vapply(x, function(y) {
    paste(sub("/.*", "", y), collapse = " ")
}, character(1L)), stringsAsFactors = FALSE)
#                                colname
# 1                few enough particular
# 2 signifcant last individual effective
0
votes
d2 <- data.frame(Col1=gsub("(?:/)[^ ]+", "",
         sapply(lst, paste, collapse=" "), perl=TRUE),
                   stringsAsFactors=FALSE)

d2
#                                   Col1
#1                 few enough particular
#2 significant last individual effective

data

 lst <-  list(c("few/JJ", "enough/JJ", "particular/JJ"),
    c("significant/JJ", "last/JJ", "individual/JJ", "effective/JJ"))