[R] cbind in for loops
David Winsemius
dwinsemius at comcast.net
Mon Jul 12 14:42:17 CEST 2010
On Jul 12, 2010, at 2:32 AM, Joshua Wiley wrote:
> Hi,
>
> Assuming that you have read the files into R,
> and that their names (in R) are held in some object
> (e.g., 'file2'), then this works
>
> do.call(what = cbind, args = mget(x = file2, envir = .GlobalEnv)
>
> Here is a reproducible example:
>
> x1 <- data.frame(x = 1:10)
> x2 <- data.frame(y = 1:10)
> file.names <- c("x1", "x2")
> do.call(cbind, mget(file.names, envir=.GlobalEnv))
>
If you want to automatically retrieve an unknown or large number of
files from the current environment you can use grep with ls
> ls()[grep("^v", ls()) ]
[1] "v1" "v2" "v3" "v4" "varname"
I don't have any objects named <...>out3 but you might try the above
form with your pattern.
--
DAvid.
> Best regards,
>
> Josh
>
>
> On Sun, Jul 11, 2010 at 9:08 PM, jd6688 <jdsignature at gmail.com> wrote:
>>
>> I have 30 files in the current directories, i would like to perform
>> the
>> cbind(fil1,file2,file3,file4....file30)
>>
>> how could i do this in a for loop:
>>
>> such as:
>> file2 <- list.files(pattern=".out3$")
>> for (j in file2) {
>> cbind(j).......how to implement cbind here
>> }
>>
>>
>> Thanks.
>>
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/cbind-in-for-loops-tp2285690p2285690.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> 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.
>>
>
>
>
> --
> Joshua Wiley
> Ph.D. Student, Health Psychology
> University of California, Los Angeles
> http://www.joshuawiley.com/
>
> ______________________________________________
> 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.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list