NorthLattitude
NorthLattitude

Reputation: 211

Concatenate n objects in a list in R

This seems like it should be a simple task, but I've been trying for hours with no luck. I think I can explain my problem without providing example data.

I am trying to concatenate objects in a list into a single object. For example, I have a series of stars objects in a list called ncdf from which I can pull elements in the basic way:

ncdf[[1]] will extract the first element
ncdf[[2]] will extract the second, and so forth.

Using stars, I am able to combine them into a single object using the base concatenate function c(). I can get the result I want manually by coding like so: y <- c(ncdf[[1]], ncdf[[2]])

My question is, I want to be able to concatenate n objects in my list. So if my list is comprised of 1000 stars objects, I obviously would not write y <- c(ncdf[[1]], ncdf[[2]],...,ncdf[[1000]] all the way to 1000. Is there a loop or some other way I could combine n list items into a single object??

Thanks so much.

Upvotes: 0

Views: 204

Answers (3)

Mwavu
Mwavu

Reputation: 2217

To add onto the provided answers, you can also use:

unlist(ncdf)

Upvotes: 3

ktiu
ktiu

Reputation: 2626

One option would be to do

Reduce(c, ncdf)

Upvotes: 3

Konrad Rudolph
Konrad Rudolph

Reputation: 545618

You can use do.call:

do.call('c', ncdf)

Upvotes: 2

Related Questions