r - merging matrix columns that exists inside a numerical list -
i have created list following 1 contains combinations of specific character inside string. code creates list follows :
library(stringr) test = str_locate_all("ttest" , "t") ind1 = lapply( lapply(1:nrow(test[[1]]), combn , x=test[[1]][,1]) , t ) ind1[[1]] = rbind(ind1[[1]], 0 )
and list i'm getting looks like
[[1]] [,1] [1,] 1 [2,] 2 [3,] 5 [4,] 0 [[2]] [,1] [,2] [1,] 1 2 [2,] 1 5 [3,] 2 5 [[3]] [,1] [,2] [,3] [1,] 1 2 5
what want combine/collapse columns (where ever more one) , unlist whole object in order create final vector c(1, 2, 5, 0, 1:2, 1:5, 2:5, 1:2:5 ) , able use expand.grid() function later.
tried solve following code partially ":" character went on different position wanted.
do.call(paste, c( as.data.frame(ind1[[2]]) ,collapse=":") ) [1] "1 2:1 5:2 5"
here idea via base r convert list elements data frames , use do.call
paste them, i.e.
unlist(lapply(ind1, function(i) do.call(paste, c(as.data.frame(i), sep = ':')))) #[1] "1" "2" "5" "0" "1:2" "1:5" "2:5" "1:2:5"
wiki
Comments
Post a Comment