[R] loop, pipe connection, output objects
jim holtman
jholtman at gmail.com
Fri Oct 13 18:11:55 CEST 2006
Why don't you read the whole file in and the use subsetting to get
your ranges instead of reading the file multiple time using 'gawk'.
You can then use 'assign' to create your objects; it would be better
to use a list.
indice <- (201:399)
result <- list()
x <- read.table('base_6_mod')
for (i in indice){
assign(paste('data.', i, sep=''), x[x[,2] >= i & x[,2] < i+1, 2:3])
# or better
result[[i]] <- x[x[,2] >= i & x[,2] < i+1, 2:3]
}
# or without loops
result <- lapply(indice, function(z) x[x[,2] >= z & x[,2] < z,])
On 10/13/06, Marco Grazzi <marco.grazzi at sssup.it> wrote:
> Hi all,
>
> I have the following -newbye- problem.
> Inside R, I am trying to process a file and creating from it many files.
> The file is organized in different columns, the second containing a code. I want to create as output objects, which contain only entries in a certain code range, and whose name contain the code itself.
> Here is my attempt
>
> indice <- (201:399)
> for(i in indice){
> data.i <- read.table(pipe(paste("gawk '{if ($2 >=", i," && $2<", i+1,") print $2,$3}' < base_6_mod " , sep='')))
> print(paste("code ...", i, " ... done"))
> }
>
> The problems are:
> 1- My sintax "data.i" does not work, and loop only produces a big "data.i" object. My goal, obviously was to have something like data.201, data.202, etc
> (second order problem)
> 2- I wonder if the sintax for the index ($2 >=", i," && $2<", i+1,") works
>
> Thanks for your help (hoping I manged to be enough clear),
> marco
> --
>
> Marco Grazzi
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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 you are trying to solve?
More information about the R-help
mailing list