Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zip or enumerate in R?

What are the R equivalents for these Python list comprehensions:

[(i,j) for i,j in zip(index, Values)] [(i,j) for i,j in enumerate(Values)] [(i,j) for i,j in enumerate(range(10,20))]   %MWE, indexing or enumerating to                                              %keep up with the index, there may                                              %be some parameter to look this up 

Example with Output

>>> [(i,j) for i,j in enumerate(range(10,20))] [(0, 10), (1, 11), (2, 12), (3, 13), (4, 14), (5, 15), (6, 16), (7, 17), (8, 18), (9, 19)] 

I have solved this problem earlier with some trick in R but cannot remember anymore, the first idea was itertools -pkg but I am hoping to find a more idiomatic way of doing things.

like image 868
hhh Avatar asked Feb 14 '12 17:02

hhh


People also ask

What is the difference between zip and enumerate?

The enumerate() function returns indexes of all items in iterables (lists, dictionaries, sets, etc.) whereas the zip() function is used to aggregate, or combine, multiple iterables.

Can you use enumerate with zip?

If you want to get the elements of multiple lists and indexes, you can use enumerate() and zip() together.

Is there an enumerate function in R?

The enumerate() function returns an iterator which returns each element of the iterable as a list with two elements: an index and the corresponding value.

Does enumerate work on tuples?

Enumerate can be used to loop over a list, tuple, dictionary, and string.


2 Answers

Answer for python enumerate:

In R, a list is ordered (see this answer). Thus, all you need is to index either keys (using names()[i]) or values (using [[i]]).

Using seq_along (alternatively can do for(i in 1:length(mylist)){...}):

> mylist <- list('a'=10,'b'=20,'c'=30) > for (i in seq_along(mylist)){ +   print(paste(i,names(mylist)[i],mylist[[i]])) + } [1] "1 a 10" [1] "2 b 20" [1] "3 c 30" 

Answer for python zip:

See one of the above answers to mimic the list of tuples. My preference is towards a data frame as shown in BondedDust's answer:

> x <- 1:3 > y <- 4:6 > data.frame(x=x, y=y)   x y 1 1 4 2 2 5 3 3 6 
like image 148
Theja Tulabandhula Avatar answered Oct 13 '22 05:10

Theja Tulabandhula


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.

like image 29
chl Avatar answered Oct 13 '22 05:10

chl