Hi there,
I know this is probably a really simple question, but without the correct
keywords, or knowledge of the correct function it is hard to search for on
the net or within R.
How do I increment a dataframe (or similar) name within a loop and assign
data?
A simple example would be:
for(i in 1:10){
test<-i
}
BUT I want it to be test[i] --- in other words I want my stored data to
be in this form:
test.1<-1
test.2<-2
test.3<-3... and so on.
I have tried doing things like:
test.i
test[i]
and have tried a paste command, for example
paste("test",i, sep="")
but this turns it into a factor and you cannot assign the data or anything.
Thanks in advance.
--
Gareth Campbell
PhD Candidate
The University of Auckland
P +649 815 3670
M +6421 256 3511
E gareth.campbell@esr.cri.nz
gcam032@gmail.com
[[alternative HTML version deleted]]
See help(list) and help("[[") ...and 'An Introduction to R'.
/Henrik
On Tue, Aug 12, 2008 at 7:49 PM, Gareth Campbell <gcam032 at gmail.com>
wrote:> Hi there,
>
> I know this is probably a really simple question, but without the correct
> keywords, or knowledge of the correct function it is hard to search for on
> the net or within R.
>
> How do I increment a dataframe (or similar) name within a loop and assign
> data?
>
> A simple example would be:
>
> for(i in 1:10){
> test<-i
> }
>
> BUT I want it to be test[i] --- in other words I want my stored data to
> be in this form:
>
> test.1<-1
> test.2<-2
> test.3<-3... and so on.
>
> I have tried doing things like:
>
> test.i
> test[i]
> and have tried a paste command, for example
>
> paste("test",i, sep="")
>
> but this turns it into a factor and you cannot assign the data or anything.
>
> Thanks in advance.
>
> --
> Gareth Campbell
> PhD Candidate
> The University of Auckland
>
> P +649 815 3670
> M +6421 256 3511
> E gareth.campbell at esr.cri.nz
> gcam032 at gmail.com
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.
>
First consider using a list:
test <- list()
for (i in 1:10) test[[i]] <- i
If you really want to create variables, then look at "assign"
for (i in 1:10) assign(paste("test.", i, sep=""), i)
On Tue, Aug 12, 2008 at 7:49 PM, Gareth Campbell <gcam032 at gmail.com>
wrote:> Hi there,
>
> I know this is probably a really simple question, but without the correct
> keywords, or knowledge of the correct function it is hard to search for on
> the net or within R.
>
> How do I increment a dataframe (or similar) name within a loop and assign
> data?
>
> A simple example would be:
>
> for(i in 1:10){
> test<-i
> }
>
> BUT I want it to be test[i] --- in other words I want my stored data to
> be in this form:
>
> test.1<-1
> test.2<-2
> test.3<-3... and so on.
>
> I have tried doing things like:
>
> test.i
> test[i]
> and have tried a paste command, for example
>
> paste("test",i, sep="")
>
> but this turns it into a factor and you cannot assign the data or anything.
>
> Thanks in advance.
>
> --
> Gareth Campbell
> PhD Candidate
> The University of Auckland
>
> P +649 815 3670
> M +6421 256 3511
> E gareth.campbell at esr.cri.nz
> gcam032 at gmail.com
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?