How to convert a list consisting of vector of different lengths to a usable data frame in R? -
i have (fairly long) list of vectors. vectors consist of russian words got using strsplit()
function on sentences.
the following head()
returns:
[[1]] [1] "модно" "создавать" "резюме" "в" "виде" [[2]] [1] "ты" "начианешь" "работать" "с" "этими" [[3]] [1] "модно" "называть" "блогер-рилейшенз" "―" "начинается" "задолго" [[4]] [1] "видел" "по" "сыну," "что" "он" [[5]] [1] "четырнадцать," "я" "поселился" "на" "улице" [[6]] [1] "широко" "продолжали" "род."
note vectors of different length.
what want able read first words each sentence, second word, third, etc.
the desired result this:
p1 p2 p3 p4 p5 p6 [1] "модно" "создавать" "резюме" "в" "виде" na [2] "ты" "начианешь" "работать" "с" "этими" na [3] "модно" "называть" "блогер-рилейшенз" "―" "начинается" "задолго" [4] "видел" "по" "сыну," "что" "он" na [5] "четырнадцать," "я" "поселился" "на" "улице" na [6] "широко" "продолжали" "род." na na na
i have tried use data.frame()
didn't work because rows of different length. tried rbind.fill()
plyr
package, function can process matrices.
i found other questions here (that's got plyr
from), combining instance 2 data frames of different size.
thanks help.
try this:
word.list <- list(letters[1:4], letters[1:5], letters[1:2], letters[1:6]) n.obs <- sapply(word.list, length) seq.max <- seq_len(max(n.obs)) mat <- t(sapply(word.list, "[", = seq.max))
the trick is, that,
c(1:2)[1:4]
returns vector + 2 nas
Comments
Post a Comment