Learn R Programming

iterors (version 1.0)

take: Return the first n elements of an iterable object in a vector.

Description

Returns the first n elements of an iterable object as a list. If n is larger than the number of elements in object, the entire iterator is consumed.

Usage

take(obj, n = 1, mode = "list", ...)

# S3 method for default take(obj, n = 1, mode = "list", ...)

# S3 method for iteror take(obj, n = 1, mode = "list", ...)

Value

a list of the first n items of the iterable obj

Arguments

obj

An iterable object.

n

The maximum number of elements to extract from the iteror.

mode

The mode of vector to return.

...

Further arguments may be passed along to the iteror constructor.

Details

A function take first appeared in package itertools2. It is basically an alias for as.list but defaults to n=1.

See Also

concat as.vector.iteror

as.vector.iteror

Examples

Run this code
take(1:10, 3) # 1 2 3
take(icount(), 10) # 1:10
take(icount(5), 10) # 1 2 3 4 5

Run the code above in your browser using DataLab