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))
?
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.
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):
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.
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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With