3

There are two vectors x: 1 3 5 7 & y: 2 4 6 8. I used the function outer to build a matrix(4*4) from all possible combinations between the elements in both vectors like:

x<-c(1,3,5,7)
y<-c(2,4,6,8)
comb<-outer(x,y, paste, sep=".")
> comb
     [,1]  [,2]  [,3]  [,4] 
[1,] "1.2" "1.4" "1.6" "1.8"
[2,] "3.2" "3.4" "3.6" "3.8"
[3,] "5.2" "5.4" "5.6" "5.8"
[4,] "7.2" "7.4" "7.6" "7.8"

I don't now if it is possible to index one element in a cell, for example if there is a way to call only the element 2 from first cell comb[1,1] Is there something like comb[[1,1]][2]=2 "I know it is not working"

And if this is not possible in this way, is there another way construct a matrix from the combinations and to be able after that to call and index each element alone??

By the way: I work on graphs and vectors of vertices so if there are other functions like this one for graphs please direct me.

Thanks

user8003788
  • 253
  • 2
  • 8

2 Answers2

1

I think outer works fine for what you need. To get the second element from your matrix use substr like this:

substr(comb[1,1],3,3)

note that I selected the 3 because it is the 3 element of the character that you need.

you can also use strsplit if there numbers with more than 1 digit in your matrix

unlist(strsplit(comb[1,1], ".", fixed = T))[2]

EDIT

If you change the matrix to a vector it would be something like this

vector_comb = c(comb)
sapply(1:length(vector_comb), function(x) unlist(strsplit(comb[x], ".", fixed = T))[2])
Alejandro Andrade
  • 2,196
  • 21
  • 40
  • Thanks for reply and answer but for a huge data how could I know what is the order of the character I am trying to call from the cell especially inside a loop?! I think it will not be efficient solution!! isn't it? are there other other solutions? – user8003788 Feb 16 '18 at 16:44
  • @user8003788 if each element of your matrix is past with a dot in the middle the second option works perfect, because it will split each element in the 2 numbers you paste and selected the second one so you know exactly what you are extracting. The substr wouldn't work that well – Alejandro Andrade Feb 16 '18 at 16:46
  • Yes it worked, thanks but I also I want to ask if I decided to convert the matrix into a vector again then is it possible to call each element alone in the cell of the vector? – user8003788 Feb 16 '18 at 17:10
  • @user8003788 if you do something as.numeric(comb) it would become a vector and then you have to do change the way you call each element, like comb[1] – Alejandro Andrade Feb 16 '18 at 17:19
  • Thanks, in this way it will read the whole cell not each element alone in the cell – user8003788 Feb 16 '18 at 17:21
  • I added an example that of how you would do it in a vector – Alejandro Andrade Feb 16 '18 at 17:32
  • Thanks a lot..it is great – user8003788 Feb 16 '18 at 17:36
  • when using this function (unlist(strsplit(comb[1,1], ".", fixed = T))[2]) and applying it on vertices in graphs how it is possible to keep the vertex name and don't mix it by the vertex number? I tried something like *names(unlist(strsplit(comb[1,1], ".", fixed = T))[2]) and like *V(g)$names(unlist(strsplit(comb[1,1], ".", fixed = T))[2]) but it didn't work!! is there another way – user8003788 Feb 20 '18 at 16:51
  • not sure, if you give me a sample of the data i will be happy to help – Alejandro Andrade Feb 20 '18 at 16:55
  • I will update the question and add another part for graph vertices – user8003788 Feb 20 '18 at 16:59
  • its better if you create an other question, its SO policy that its one question per thread – Alejandro Andrade Feb 20 '18 at 17:03
  • Ok I will do and I will add a link to this question, thanks for the advice – user8003788 Feb 20 '18 at 17:04
0

Is this what you need ? By using Vectorize

comb<-outer(x,y, Vectorize( function(a,b) c( as.list(a), as.list(b) ),SIMPLIFY = FALSE))
comb
     [,1]   [,2]   [,3]   [,4]  
[1,] List,2 List,2 List,2 List,2
[2,] List,2 List,2 List,2 List,2
[3,] List,2 List,2 List,2 List,2
[4,] List,2 List,2 List,2 List,2
comb[[1,1]][2]
[[1]]
[1] 2

comb[[1,1]][1]
[[1]]
[1] 1

Update :

unlist(strsplit(comb[1,1],'.',fixed = T))[1]
[1] "1"
BENY
  • 317,841
  • 20
  • 164
  • 234
  • Thanks a lot but is it possible to show this matrix by filled by the elements themselves? i.e the form like my matrix above but to be able to use your index? – user8003788 Feb 16 '18 at 16:51
  • @user8003788 base on you description , you need a list in every cell , so that is join type , which I usually do not recommend . And the view of the list item come with the problem you just mention above (can not show on view table ) – BENY Feb 16 '18 at 16:55
  • @Wen whats the differences between fixed = 2 and fixed = T? – Alejandro Andrade Feb 16 '18 at 17:03
  • @AlejandroAndrade it is my bad , we should using fixed =TRUE – BENY Feb 16 '18 at 17:06
  • Ok, I understand you.. Excuse for this and if I decided to convert the matrix into a vector again then is it possible to call each element alone in the cell of the vector? – user8003788 Feb 16 '18 at 17:10
  • @user8003788 if you chose `unlist(strsplit(comb[1,1],'.',fixed = T))[1]` you do not need , it is base on `comb<-outer(x,y, paste, sep=".")` – BENY Feb 16 '18 at 17:13
  • @user8003788 yw~ happy coding – BENY Feb 16 '18 at 17:23
  • @Wen, if I applied this function (unlist(strsplit(comb[1,1], ".", fixed = T))[2]) on graph vertices then how it is possible to keep the vertex name and don't mix it by the vertex number? I applied this one *names(unlist(strsplit(comb[1,1], ".", fixed = T))[2]) and this one *V(g)$names(unlist(strsplit(comb[1,1], ".", fixed = T))[2]) and I failed, is there something else? – user8003788 Feb 20 '18 at 16:55