python 2 R

Published onesixx on

https://stackoverflow.com/questions/9281323/zip-or-enumerate-in-r

There have been some discussions around “list comprehension for R”,  e.g. here or  there.

The hash package even offers dictionary-like structure. However, as others said, it is difficult to try to map one language facilities onto another (even if this is what Comparison of programming languages actually offers) without a clear understanding of what it is supposed to be used to.

 

For example, I can mimic Python zip() in R as follows:

Python

In [1]: x = [1,2,3]
In [2]: y = [4,5,6]

In [3]: zip(x, y)
Out[3]: [(1, 4), (2, 5), (3, 6)]

 

R

> x <- 1:3
> y <- 4:6

> list(x, y)                     # gives a simple list

> as.list(paste(x, y))           # three tuples, as a list of characters

> mapply(list, x, y, SIMPLIFY=F) # gives a list of 3 tuples

> rbind(x, y)                    # gives a 2x3 matrix 

As can be seen, this really depends on what you want to do with the result afterwards.

 

 

 

Categories: R Basic

onesixx

Blog Owner

Leave a Reply

Your email address will not be published.