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]]
It's definitely possible to create R objects without knowing their names using 'assign', but this is considered bad practice. For example, assign("tay", riverlist[["tay"]]) And you could put that in a for loop, looping through the names of your files. You could consider using 'attach'. You would write something like attach(riverlist) and it would assign your variables into a new environment and put it on your search list. Only problem is that modifying these objects will (generally) create new objects, and not modify the originals. For example, tay[[1]] <- NA will create a new variable tay in your global environment, and not modify the variable in your riverlist environment. Maybe that's how you want it to work, up to you. In general though, I would say that making variables in either of these manners is setting yourself up for failure. It is much better to do tay <- read.csv(...) forth <- read.csv(...) ... One final solution is to do what you're currently doing, but use names to refer to a data frame in a list, instead of integers. For example, if your csv files are "tay.csv", "forth.csv", and "don.csv", and you had those strings stored in a variable 'riverfiles' then remove the .csv and give the riverlist some names like names(riverlist) <- sub("[.]csv$", "", riverfiles) I hope this helps. On Sun, Mar 20, 2022, 18:03 Nick Wray <nickmwray at gmail.com> wrote:> 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 at 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. >[[alternative HTML version deleted]]
Unless you want to make some transformation like tay/don it might work better to make a new variable with catchment and append one dataset to the other. If later you decide that was a bad choice, use pivot_wider to reorganize the data. Tim -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Nick Wray Sent: Sunday, March 20, 2022 6:03 PM To: r-help at r-project.org Subject: [R] Naming files within R code [External Email] 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=e3PR6lu4RXxbI3pz4G9YTtjhx4hngiq8Ri1ZGN8Zh1BnxmLp96ohrDR_XciLNAR5&s=h6PbgaQbzT184NTXJtdTDvmyk_W8ryszwocFp1o_yyA&ePLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=e3PR6lu4RXxbI3pz4G9YTtjhx4hngiq8Ri1ZGN8Zh1BnxmLp96ohrDR_XciLNAR5&s=sLKcHu2BJ_iU2JW3WjaWFLlLYQaIQMTMvlwYWwRjm9M&eand provide commented, minimal, self-contained, reproducible code.
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 at 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.