How to convert one list of columns to a matrix of elements in R?

Currently, I have data that is in the following format (note, this is 1 column, 4 rows of the matrix):

aa|bb bb|cc|ee|ee cc cc|ee 

and I want it to be displayed so that the column names are: aa, bb, cc, dd and ee. And I want there to be 4 lines, each line counts the number of lines that each line was present in the corresponding line above.

ie)

 aa bb cc dd ee 1 1 0 0 0 0 1 1 0 2 0 0 1 0 0 0 0 1 0 1 

Does anyone know how to do this in R? I would post my attempt, but it just gets ugly and complicated. Any help would be greatly appreciated.

Thanks in advance.

+4
source share
1 answer

Here is an idea:

 # (You'll use as.vector() on your matrix to get the vector x.) x <- c("aa|bb", "bb|cc|ee|ee", "cc", "cc|ee") levs <- c("aa", "bb", "cc", "dd", "ee") ll <- strsplit(x, "\\|") t(sapply(ll, function(X) table(c(levs, X)))) - 1 # aa bb cc dd ee # [1,] 1 1 0 0 0 # [2,] 0 1 1 0 2 # [3,] 0 0 1 0 0 # [4,] 0 0 1 0 1 

This may explain (at least a little) what this last line of code does:

 table(c(levs, c("dd", "cc", "cc", "cc"))) - 1 # # aa bb cc dd ee # 0 0 3 1 0 
+4
source

Source: https://habr.com/ru/post/1442418/


All Articles