[R] Creating a list of combinations
baptiste auguie
baptiste.auguie at googlemail.com
Thu Aug 20 14:11:11 CEST 2009
the short answer is to add [[i]] in your loop,
file_list[[i]] <- paste(index$month[i], index$year[i], sep='')
yet a shorter answer would be,
file_list = apply(index, 1, paste, collapse="")
HTH,
baptiste
2009/8/20 Steve Murray <smurray444 at hotmail.com>:
>
> Dear R Users,
>
> I have 120 objects stored in R's memory and I want to pass the names of these many objects to be held as just one single object. The naming convention is month, year in sequence for all months between January 1986 to December 1995 (e.g. Jan86, Feb86, Mar86... through to Dec95). I hope to pass all these names (and their data I guess) to an object called file_list, however, I'm experiencing some problems whereby only the first (and possibly last) names seem to make the list, with the remainder recorded as 'NA' values.
>
> Here is my code as it stands:
>
> index <- expand.grid(month=month.abb, year=seq(from=86,to=95, by=1))
>
> for (i in seq(nrow(index))) {
> file_list <- paste(index$month[i], index$year[i], sep='')
> print(file_list[i])
> }
>
> Output is as follows:
>
> [1] "Jan86"
> [1] NA
> [1] NA
> [1] NA
> #[continues to row 120 as NA]
>
>> file_list; file_list[i]
> [1] "Dec95"
> [1] NA
>
>> head(index) # this seems to be working fine
> month year
> 1 Jan 86
> 2 Feb 86
> 3 Mar 86
> 4 Apr 86
> 5 May 86
> 6 Jun 86
>
>
> Any help on how I can populate file_list correctly with all 120 combinations of month + year (without NAs!) would be gratefully received.
>
> Thanks,
>
> Steve
>
>
>
> _________________________________________________________________
>
> icons.
>
> ______________________________________________
> 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.
>
--
_____________________________
Baptiste Auguié
School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK
http://newton.ex.ac.uk/research/emag
More information about the R-help
mailing list