Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating tuples from two vectors

Tags:

If I have two vectors of the same length A<-c(5,10) and B<-c(7,13) how can I easily turn these two vectors into a single tuple vector i. e. c((5,7),(7,13))?

like image 764
LostLin Avatar asked Oct 17 '11 21:10

LostLin


People also ask

How to create a tuple in Python?

How to create a tuple in Python? To create a tuple in Python, add items with round brackets “()” like my_tuple = (“red”, “blue”, “green”) and to create an empty tuple, use empty round brackets ” ” with no items in it.

How to merge two lists into a list of tuples?

Python | Merge two lists into list of tuples. Given two lists, write a Python program to merge the two lists into list of tuples. Examples: Merge both the list into a list of tuple using a for loop. But the drawback is given two lists need to be of the same length. def merge(list1, list2):

How to assign the value of two existing tuples to a new tuple?

Here, we assign the value of two existing tuples to a new tuple by using the ‘ +’ operator. In python, to check if the specified element is present in the tuple we use the ” in “ keyword to check and if the element is not present then it will not return anything.

Is this a valid syntax for a tuple vector c (5 7)?

Your tuple vector c ( (5,7), (7,13)) is not valid syntax. However, your phrasing makes me think you are thinking of something like python's zip. How do you want your tuples represented in R?


1 Answers

Others have mentioned lists. I see other possibilities:

cbind(A, B)  # makes a column-major  2x2-"vector" rbind(A, B)  # an row major 2x2-"vector" which could also be added to an array with `abind` 

It is also possible to preserve their "origins"

AB <- cbind(A=A, B=B) array(c(AB,AB+10), c(2,2,2) ) , , 1      [,1] [,2] [1,]    5    7 [2,]   10   13 , , 2      [,1] [,2] [1,]   15   17 [2,]   20   23  > abind( array(c(AB,AB+10), c(2,2,2) ), AB+20) , , 1       A  B [1,]  5  7 [2,] 10 13  , , 2       A  B [1,] 15 17 [2,] 20 23  , , 3       A  B [1,] 25 27 [2,] 30 33 
like image 127
IRTFM Avatar answered Oct 18 '22 14:10

IRTFM