[R] Naming files within R code

Eric Berger er|cjberger @end|ng |rom gm@||@com
Mon Mar 21 05:47:13 CET 2022


A different approach is to give names to the list elements and use a
shorter name for the list.
Then the code looks close to what you want without actually changing
its structure.

names(riverlist) <- c("tay","forth","don")
l <- riverlist
print(l$tay[,1])

HTH,
Eric


On Mon, Mar 21, 2022 at 12:53 AM Rui Barradas <ruipbarradas using sapo.pt> wrote:
>
> Hello,
>
> You can read in the files in a `lapply` loop, assign the result to a
> list, say df_list, and
>
> names(df_list) <- c("tay","forth","don")
> list2env(df_list, envir = globalenv())
>
>
> This creates 3 data.frames with those names in the global environment.
>
> Hope this helps,
>
> Rui Barradas
>
> Às 22:02 de 20/03/2022, Nick Wray escreveu:
> > Hello    I have data from various Scottish river catchments in the form of
> > csv files.  I want to be able to download the files in turn and refer to
> > each one by an assigned name from a vector of names, but within R.  So, for
> > example, if my vector of names is c("tay","forth","don") I want to tell R
> > to refer to the sequences of dataframes as a variable name, ie the table
> > uploaded from the first csv becomes the object tay *within* R and so on, so
> > that I could the do things like write print(tay[,1]) etc,rather than having
> > to refer to a list eg print(riverlist[[1]][,1])  I don't know whether this
> > is possible but if it is I'd be grateful for any pointers
> > Thanks, Nick Wray
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list