Hi!
I want to write portions of my data (3573 columns at a time) to twenty
folders I have available titled "A_1" to "A_20" such that
the first 3573
columns will go to folder A_1, next 3573 to folder A_2 and so on.
This code below ensures that the data is written into all 20 folders, but
only the last iteration of the loop (last 3573 columns) is being written
into ALL of the folders (A_1 to A_20) rather than the sequential order that
I would like.
How can I fix this?
Thank you!
*************************
Code:
A_var_df <- data.frame(index=1:length(seq(1.0, -0.9, by= -0.1)),
from=seq(1.0, -0.9, by= -0.1), to=seq(0.9, -1.0, by= -0.1))
for(i in 1:length(A_var_df[,1]))
{
library(plyr)
max.col <- ncol(Dchr1)
l_ply(seq(1, max.col, by=3573), function(k)
write.table(as.data.frame(Dchr1[,k:min(k+3572, max.col)]),
paste("./A_",
A_var_df[i,1], "/k.csv", sep=""), sep=",",
row.names=F, quote=F) )
}
**************************
--
Thanks,
CC
[[alternative HTML version deleted]]
Joris Meys
2010-Jul-07 21:21 UTC
[R] problems with write.table, involving loops & paste statement
To be correct, everything is written to all folders according to my testing.
There is absolutely no need whatsoever to use l_ply. And in any case,
take as much as possible outside the loop, like the library statement
and the max.col.
Following is _a_ solution, not the most optimal, but as close as
feasible to your code :
A_var_df <- data.frame(index=1:length(seq(1.0, -0.9, by= -0.1)),
from=seq(1.0, -0.9, by= -0.1), to=seq(0.9, -1.0, by= -0.1))
# I make some data and make sure I can adjust the number of dirs and the steps
Dchr1 <-matrix(rep(1:100,each=10),ncol=100)
dirs <- 20
max.col <- ncol(Dchr1)
steps = ceiling(max.col/dirs)
cols <- seq(1, max.col, by=steps)
for(i in 1:length(A_var_df[,1]))
{
k <- cols[i]
print(as.data.frame(Dchr1[,k:min(k+steps, max.col)]))
print(paste("./A_",A_var_df[i,1], "/k.csv",
sep=""))
# I use print here just to show which dataframe is going to which directory,
# you can reconstruct the write.table yourself.
}
Cheers
Joris
On Wed, Jul 7, 2010 at 9:32 PM, CC <turtysmail at gmail.com>
wrote:> Hi!
>
> I want to write portions of my data (3573 columns at a time) to twenty
> folders I have available titled "A_1" to "A_20" such
that the first 3573
> columns will go to folder A_1, next 3573 to folder A_2 and so on.
>
> This code below ensures that the data is written into all 20 folders, but
> only the last iteration of the loop (last 3573 columns) is being written
> into ALL of the folders (A_1 to A_20) rather than the sequential order that
> I would like.
>
> How can I fix this?
>
> Thank you!
>
> *************************
>
> Code:
>
> A_var_df <- data.frame(index=1:length(seq(1.0, -0.9, by= -0.1)),
> from=seq(1.0, -0.9, by= -0.1), to=seq(0.9, -1.0, by= -0.1))
>
> for(i in 1:length(A_var_df[,1]))
> {
> library(plyr)
> max.col <- ncol(Dchr1)
> l_ply(seq(1, max.col, by=3573), function(k)
> write.table(as.data.frame(Dchr1[,k:min(k+3572, max.col)]),
paste("./A_",
> A_var_df[i,1], "/k.csv", sep=""), sep=",",
row.names=F, quote=F) )
> }
>
> **************************
>
> --
> Thanks,
> CC
>
> ? ? ? ?[[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.
>
--
Joris Meys
Statistical consultant
Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control
tel : +32 9 264 59 87
Joris.Meys at Ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php