Hello R list. I'm very new. I have what I hope is a simple question that I
have not been able to find a good solution for.
If this is common and clutters anyone's inbox my most sincere apologies in
advance...
I am trying to use an argument from a function in the name of a data.frame. But
I seem to only be able to reference the arguments from the right hand side of
the <- .
So something like this. I would just want the function below to create a new
data.frame named "myDataSet".
Done on its own like "myDataSet <- data.frame()"
myFunction <- function(x){ # suppose x is just a collection with one
value with is the name I want the data.frame create as..
c("myDataSet") for example
name(x) <- data.frame()
}
But this of course doesn't work because I have not figured out how to
reference the function argument from the left hand side of the <- .
I have read about the "assign" function but from what I can tell
(granted I'm new to R) the assign function works on the variable names not
data.frame names which is what I need.
Ultimately what I'm trying accomplish to send a list of character strings
then iterate it and create multiple new data.frames each having the name of one
of the elements of the list passed to the function.
If there is even a better article out on the web that I'm obviously missing
please orient me. I have struck out so far but I know I may not be searching
correctly either.
Any help at all would be much appreciated...
While the assign function is in fact the function you are looking for, I would
strongly advise that you cease and desist in this endeavour and instead make a
list of data frames rather than littering your global environment with many
individual data frames.
If you have a vector of names of files you can use lapply to read them all into
a list of data frames. You can then use the filenames as "names" with
which to look up the appropriate data. E.g.
dtadir <- "datadir"
myfiles <- list.files( dtadir )
dtalist <- lapply( myfiles, function(fn) {
read.csv( file.path( dtadir, fn ), stringsAsFactors=FALSE )
}
names( dtalist ) <- myfiles
str(dtalist$file001.csv)
str(dtalist[[ "file001.csv" ]])
str( dtalist[[ 1 ]] )
If the filenames have unusual characters in them then you may have to use
back-tick quotes when using the dollar-sign operator.
On April 15, 2019 8:50:44 AM PDT, "Fieck, Joe" <Joe.Fieck at
Teradata.com> wrote:>Hello R list. I'm very new. I have what I hope is a simple question
>that I have not been able to find a good solution for.
>If this is common and clutters anyone's inbox my most sincere apologies
>in advance...
>
>I am trying to use an argument from a function in the name of a
>data.frame. But I seem to only be able to reference the arguments from
>the right hand side of the <- .
>
>So something like this. I would just want the function below to create
>a new data.frame named "myDataSet".
>Done on its own like "myDataSet <- data.frame()"
>
>myFunction <- function(x){ # suppose x is just a collection with
>one value with is the name I want the data.frame create as..
>c("myDataSet") for example
> name(x) <- data.frame()
>}
>
>But this of course doesn't work because I have not figured out how to
>reference the function argument from the left hand side of the <- .
>I have read about the "assign" function but from what I can tell
>(granted I'm new to R) the assign function works on the variable names
>not data.frame names which is what I need.
>
>Ultimately what I'm trying accomplish to send a list of character
>strings then iterate it and create multiple new data.frames each having
>the name of one of the elements of the list passed to the function.
>
>If there is even a better article out on the web that I'm obviously
>missing please orient me. I have struck out so far but I know I may
>not be searching correctly either.
>Any help at all would be much appreciated...
>______________________________________________
>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.
--
Sent from my phone. Please excuse my brevity.
Thanks for the reply Jeff. I will play around with your code example and see
where it takes me.
-----Original Message-----
From: Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
Sent: Monday, April 15, 2019 4:55 PM
To: r-help at r-project.org; Fieck, Joe <Joe.Fieck at Teradata.com>;
R-help at r-project.org
Subject: Re: [R] Code driven data.frame naming question.
[External Email]
________________________________
While the assign function is in fact the function you are looking for, I would
strongly advise that you cease and desist in this endeavour and instead make a
list of data frames rather than littering your global environment with many
individual data frames.
If you have a vector of names of files you can use lapply to read them all into
a list of data frames. You can then use the filenames as "names" with
which to look up the appropriate data. E.g.
dtadir <- "datadir"
myfiles <- list.files( dtadir )
dtalist <- lapply( myfiles, function(fn) {
read.csv( file.path( dtadir, fn ), stringsAsFactors=FALSE )
}
names( dtalist ) <- myfiles
str(dtalist$file001.csv)
str(dtalist[[ "file001.csv" ]])
str( dtalist[[ 1 ]] )
If the filenames have unusual characters in them then you may have to use
back-tick quotes when using the dollar-sign operator.
On April 15, 2019 8:50:44 AM PDT, "Fieck, Joe" <Joe.Fieck at
Teradata.com> wrote:>Hello R list. I'm very new. I have what I hope is a simple question
>that I have not been able to find a good solution for.
>If this is common and clutters anyone's inbox my most sincere apologies
>in advance...
>
>I am trying to use an argument from a function in the name of a
>data.frame. But I seem to only be able to reference the arguments from
>the right hand side of the <- .
>
>So something like this. I would just want the function below to create
>a new data.frame named "myDataSet".
>Done on its own like "myDataSet <- data.frame()"
>
>myFunction <- function(x){ # suppose x is just a collection with
>one value with is the name I want the data.frame create as..
>c("myDataSet") for example
> name(x) <- data.frame()
>}
>
>But this of course doesn't work because I have not figured out how to
>reference the function argument from the left hand side of the <- .
>I have read about the "assign" function but from what I can tell
>(granted I'm new to R) the assign function works on the variable names
>not data.frame names which is what I need.
>
>Ultimately what I'm trying accomplish to send a list of character
>strings then iterate it and create multiple new data.frames each having
>the name of one of the elements of the list passed to the function.
>
>If there is even a better article out on the web that I'm obviously
>missing please orient me. I have struck out so far but I know I may
>not be searching correctly either.
>Any help at all would be much appreciated...
>______________________________________________
>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.
--
Sent from my phone. Please excuse my brevity.