using an element of an array as a new object

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

using an element of an array as a new object

ppaarrkk
I have an array called filesBox. I want to take each element of the first column and assign a dataset to it.

For example :

filesBox[4,1]  returns

[1] "fileR"

Then I want to assign "fileR" which exists as a text file to the R object "fileR" like this :
filesBox[4,1] <- read.table(paste(dir2, filesBox[4,1], sep=""), header = FALSE)

But this doesn't work because filesBox[4,1] is an element of an array but I want it as a new R (effectively) data frame.

Any ideas please ?
Reply | Threaded
Open this post in threaded view
|

Re: using an element of an array as a new object

Julian Burgos
See ?assign.

ppaarrkk wrote:

> I have an array called filesBox. I want to take each element of the first
> column and assign a dataset to it.
>
> For example :
>
> filesBox[4,1]  returns
>
> [1] "fileR"
>
> Then I want to assign "fileR" which exists as a text file to the R object
> "fileR" like this :
> filesBox[4,1] <- read.table(paste(dir2, filesBox[4,1], sep=""), header =
> FALSE)
>
> But this doesn't work because filesBox[4,1] is an element of an array but I
> want it as a new R (effectively) data frame.
>
> Any ideas please ?

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: using an element of an array as a new object

Greg Snow-2
In reply to this post by ppaarrkk
I would work with 2 different objects to be safe (you can rename the 2nd
after everything works if you want).

Try something like:

> filesBox2 <- array( list(), dim(filesBox) )
> filesBox2[4,1] <- read.table(paste(dir2, filesBox[4,1], sep=""),
header = FALSE)
.
.
.

Hope this helps,

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
[hidden email]
(801) 408-8111
 
 

> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of ppaarrkk
> Sent: Wednesday, January 16, 2008 9:16 AM
> To: [hidden email]
> Subject: [R] using an element of an array as a new object
>
>
> I have an array called filesBox. I want to take each element
> of the first column and assign a dataset to it.
>
> For example :
>
> filesBox[4,1]  returns
>
> [1] "fileR"
>
> Then I want to assign "fileR" which exists as a text file to
> the R object "fileR" like this :
> filesBox[4,1] <- read.table(paste(dir2, filesBox[4,1],
> sep=""), header =
> FALSE)
>
> But this doesn't work because filesBox[4,1] is an element of
> an array but I want it as a new R (effectively) data frame.
>
> Any ideas please ?
> --
> View this message in context:
> http://www.nabble.com/using-an-element-of-an-array-as-a-new-ob
> ject-tp14884435p14884435.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: using an element of an array as a new object

ppaarrkk
Thankyou for the replies.

assign() works.

for (i in 1:7)
assign(filesBox[i,1],read.table(paste(dir2, filesBox[i,1], sep=""), header = FALSE))