0

I'm working with a list, in which I've split strings, so each elements can have different numbers split charaters. I'd like this is a matrix, in which if missing characters, should be null.

 test_data <- cbind(c("laptop", "lenovo", "apple watch", "fitbit", "app radio", "lock"), c("10", "5", "100", "25", "2", "2"))

 tester <- strsplit(test_data[,1],"")

print (tester)

[[1]]
[1] "l" "a" "p" "t" "o" "p"

[[2]]
[1] "l" "e" "n" "o" "v" "o"

[[3]]
[1] "a" "p" "p" "l" "e" " " "w" "a" "t" "c" "h"

[[4]]
[1] "f" "i" "t" "b" "i" "t"

[[5]]
[1] "a" "p" "p" " " "r" "a" "d" "i" "o"

[[6]]
[1] "l" "o" "c" "k"

I tried this:

 matrix( tester , ncol = (max(nchar(test_data[,1]))) , byrow = TRUE )

But this result isn't what I'm looking for.

      [,1]        [,2]        [,3]         [,4]        [,5]        [,6]        [,7]        [,8]        [,9]         [,10]      
[1,] Character,6 Character,6 Character,11 Character,6 Character,9 Character,4 Character,6 Character,6 Character,11 Character,6
  [,11]      
  [1,] Character,9

Here is the first row of the desired output:

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]  [,9]  [,10] [,11]      
  [1,] "l"  "a"   "p"  "t"  "o"  "p"  NA NA NA NA NA
jKraut
  • 2,325
  • 6
  • 35
  • 48
  • Please provide the expected output. – count Mar 17 '16 at 19:35
  • 3
    You can check `stri_list2matrix` from `library(stringi)` – akrun Mar 17 '16 at 19:39
  • 1
    In `base` R, try `do.call(rbind,lapply(tester,"length<-",max(lengths(tester))))`. Remember that `NULL` can't be an element of a `character` vector (or matrix). Missing values in vectors are denoted with `NA` in R. – nicola Mar 17 '16 at 19:49

0 Answers0