outside save from loop

How can I save in one dataframe or in any other form outside the loop all my data?? I have bulk .nc files, I don't have problem getting the values or creating the data frame. The loop gives me 500 different d ( explanation, d=rbind(df, df.smos.s1)) but I would like to have one single file that will have all the information of the d.
Any idea??

df=NULL
files <- list.files(path="~/data", pattern="*.nc", full.names=T)
for (i in 1:length(files)){
nc <- open.nc(files[i])
nc_lon = var.get.nc(nc, "Longitude")
nc_lat=var.get.nc(nc, "Latitude")
nc_sm=var.get.nc(nc, "Soil_Moisture")
nc_tm=var.get.nc(nc, "Days")
df.smos.s1= data.frame(nc_lon,nc_lat,nc_sm,nc_tm)
d = rbind(df, df.smos.s1)
print(d) #500 times different .nc files
}

Hi Varvara,

It looks like you should move the print(d) statement to place it outside the for loop, i.e. print(d) one line down and curly bracket one line up.

Within the for loop you read all the files and append the results to d using rbind(), and only after completing this loop you should print(d).

Could you please test this? Does it work?

BR,

Egge-Jan

If I change place the print(d), gives me only one result, the last one of .nc files that read in loop. I use the print (d), inside the loop only to see if runs well the code.
My final goal is to have only one file, that will be saved, with the values which was extracted/ get it from all my .nc files.

Hi @Varvara! Welcome!

Each iteration of your loop is overwriting d, instead of adding to it. So at the end of the loop, d has the value of the last time it was created (the last loop iteration). If you mean to be updating df, then you’d write:

df = rbind(df, df.smos.s1)

If you want your final combined object to be named d, then you’d change the line at the beginning to

d = NULL

And the later line to:

d = rbind(d, df.smos.s1)

As a note, it’s possible to do this sort of task in R without using a for loop (or, to be more precise, using functions whose underlying loops are written by somebody else, often in a more optimized way). The base R idiom here would be lapply() plus do.call(rbind, ...). In the tidyverse idiom, you’d use purrr::map_dfr().

Another note: we ask posters here to properly format their code. It makes the site easier to read, prevents confusion (unformatted code can mistakenly trigger other types of automatic formatting) and generally is considered the polite thing to do. You can read all the details of how to do it here: FAQ: How to make your code look nice? Markdown Formatting

Or try the easy way :grin: : select code (in blocks or in bits and pieces mixed into your sentences) and click the little </> button at the top of the posting box.

3 Likes