[R] save output of loop

2012-02-14 Thread uday
I have some data files e.g 100 . and after for loop I would like to save all
data in one single data frame 

file_s - list.files(path = ., pattern = v2.0.2.txt, all.files = FALSE,
  full.names = FALSE, recursive = FALSE,
  ignore.case = FALSE)
for (i in 1:100){ 
  data = read.table(file_s[i],header=TRUE) 
  lat  = data[,7] # latitude
  lon = data[,8] # longitude
  gas  = data[,45] # gas 
  time.s   = data[,5] # time 
}

How I should get all these 100 files variable in to single data frame ? 


--
View this message in context: 
http://r.789695.n4.nabble.com/save-output-of-loop-tp4386599p4386599.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] save output of loop

2012-02-14 Thread jim holtman
try this:

file_s - list.files(path = ., pattern = v2.0.2.txt, all.files = FALSE,
 full.names = FALSE, recursive = FALSE,
 ignore.case = FALSE)
result - do.call(rbind, lapply(file_s, function(.file){
data - read.table(.file, header=TRUE)
data.frame(lat  = data[,7] # latitude
, lon = data[,8] # longitude
, gas  = data[,45] # gas
, time.s   = data[,5] # time
, stringsAsFactors = FALSE
)
}))



On Tue, Feb 14, 2012 at 5:07 AM, uday uday_143...@hotmail.com wrote:
 I have some data files e.g 100 . and after for loop I would like to save all
 data in one single data frame

 file_s - list.files(path = ., pattern = v2.0.2.txt, all.files = FALSE,
                  full.names = FALSE, recursive = FALSE,
                  ignore.case = FALSE)
 for (i in 1:100){
  data     = read.table(file_s[i],header=TRUE)
  lat  = data[,7] # latitude
  lon = data[,8] # longitude
  gas  = data[,45] # gas
  time.s   = data[,5] # time
 }

 How I should get all these 100 files variable in to single data frame ?


 --
 View this message in context: 
 http://r.789695.n4.nabble.com/save-output-of-loop-tp4386599p4386599.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@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
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

__
R-help@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.


Re: [R] save output of loop

2012-02-14 Thread johnmark
The short answer to your question is *don't* concatenate the values in the
row, then attempt to /rbind()/ them incrementally to a data.frame.  Instead
build each column separately inside the loop, then /cbind() (data.frame()/ 
does an implicit/ cbind()/ ) them together at the end.  Something like this:

/
lat.column - c(length(100))
lon.column - c(length(100))
...
for (i in 1:100){ 
 ...
  lat.column[i]  - data[,7] # latitude 
  lon.column[i] - data[,8] # longitude 
   ...
} 
my.data - data.frame(lat.column, lon.column, ...)
/

-jm


--
View this message in context: 
http://r.789695.n4.nabble.com/save-output-of-loop-tp4386599p4387476.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] save output of loop

2012-02-14 Thread uday
Hi it works fine 
thanks 

--
View this message in context: 
http://r.789695.n4.nabble.com/save-output-of-loop-tp4386599p4387806.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] save output of loop

2012-02-14 Thread uday
The dimensions of variables are unknown , they changes to every file. 

--
View this message in context: 
http://r.789695.n4.nabble.com/save-output-of-loop-tp4386599p4387804.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] save in for loop

2010-06-04 Thread Joris Meys
On a side note:

On Thu, May 20, 2010 at 9:43 AM, Ivan Calandra
ivan.calan...@uni-hamburg.de wrote:
 Thanks to all of you for your answers!

 ...

 Tao, I don't understand why you have backslashes before file and after
 .rda. I guess it's something about regular expression, but I'm still
 very new to it.
 eval(parse(text=paste(save(file, i, , file=\file, i, .rda\),
 sep=)))

Very simple: You need to give a command as a string. In the save
command, you have to put quotation marks around the filename. Now
within the paste function, a simple quotation mark would make R
believe the string to paste ends there, and you don't want that. So
you escape the  by typing \, then R knows you want to add the symbol
 to the string instead of end it.  :

 paste(save(file, i, , file=\file, i, .rda\),sep=)
[1] save(file2, file=\file2.rda\)

 parse(text=paste(save(file, i, , file=\file, i, .rda\),sep=))

expression(save(file2, file=file2.rda))
attr(,srcfile)
text

 paste(save(file, i, , file=file, i, .rda),sep=)
Error: unexpected symbol in paste(save(file, i, , file=file

Hope it's a bit more clear now.
Cheers
Joris
-- 
Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

tel : +32 9 264 59 87
joris.m...@ugent.be
---
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

__
R-help@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.


Re: [R] save in for loop

2010-05-20 Thread Ivan Calandra
Thanks to all of you for your answers!

Peter's is definitely the easiest :)
for (i in 1:4) {
   temp - data.frame(a=(i+1):(i+10), b=LETTERS[(i+1):(i+10)])
   filename - paste(file, i, sep=)
   assign(filename, temp)
   save(list=c(filename), file=paste(filename, .rda, sep=))
}

Tao, I don't understand why you have backslashes before file and after 
.rda. I guess it's something about regular expression, but I'm still 
very new to it.
eval(parse(text=paste(save(file, i, , file=\file, i, .rda\), 
sep=)))

Jorge, your solution does not work... I've just copy/pasted your code. 
My second great weakness is with the apply() family. So maybe I have to 
adjust some part of the code to my needs, but I'm unable to do it.
i - 1:4
sapply(i, function(i) {
   x - data.frame(a=(i+1):(i+10), b=LETTERS[(i+1):(i+10)])
save(x, file = paste(file, i, .rda, sep=))
} )


Anyway, everything's now fine!
Thanks again.
Ivan


Le 5/19/2010 20:29, Jorge Ivan Velez a écrit :
 Hi Ivan,

 How about this?

 i - 1:4
 sapply(i,
 function(i){
 x - data.frame(a=(i+1):(i+10), b=LETTERS[(i+1):(i+10)])
 save(x, file = paste(file, i, .rda, sep=))
 }
 )

 HTH,
 Jorge

Le 5/19/2010 22:20, Peter Ehlers a écrit :
 On 2010-05-19 12:05, Shi, Tao wrote:
 Ivan,

 Try this:

 eval(parse(text=paste(save(file, i, , file=\file, i, 
 .RData\), sep=)))

 ...Tao


 Or just use 'list=' like this:

 for (i in 1:4) {
   temp - data.frame(a=(i+1):(i+10), b=LETTERS[(i+1):(i+10)])
   filename - paste(file, i, sep=)
   assign(filename, temp)
   save(list=c(filename), file=paste(filename, .rda, sep=))
 }

  -Peter Ehlers



 - Original Message 
 From: Ivan Calandraivan.calan...@uni-hamburg.de
 To: r-help@r-project.org
 Sent: Wed, May 19, 2010 7:56:44 AM
 Subject: [R] save in for loop

 Dear users,

 My problem concerns save() within a for loop.
 Here is my
 code:

 for (i in 1:4) {
 temp- data.frame(a=(i+1):(i+10),
 b=LETTERS[(i+1):(i+10)])
 filename- paste(file, i, sep=)

 assign(filename, temp)
 save(filename, file=paste(filename, .rda,
 sep=))
 }

 As you can see, save() doesn't work as I would like: (1)
 the object saved is called filename (instead of file1, file2, 
 etc), and
 (2) it of course contains only the name (as character) instead of the
 data.frame

 How can I fix it?

 [snip]


-- 
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calan...@uni-hamburg.de

**
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] save in for loop

2010-05-20 Thread Ivan Calandra
Hi Dennis,

What is the problem with using eval(parse(text=...))? Is there a reason 
why not to use it?
Of course, in that case, list within save() is much easier and works 
perfectly.

In any case, thanks for your explanations :)
Ivan

Le 5/20/2010 10:26, Dennis Murphy a écrit :
 Hi:

 On Thu, May 20, 2010 at 12:43 AM, Ivan Calandra 
 ivan.calan...@uni-hamburg.de mailto:ivan.calan...@uni-hamburg.de 
 wrote:

 Thanks to all of you for your answers!

 Peter's is definitely the easiest :)
 for (i in 1:4) {
   temp - data.frame(a=(i+1):(i+10), b=LETTERS[(i+1):(i+10)])
   filename - paste(file, i, sep=)
   assign(filename, temp)
   save(list=c(filename), file=paste(filename, .rda, sep=))
 }



 Tao, I don't understand why you have backslashes before file and after

 .rda. I guess it's something about regular expression, but I'm still
 very new to it.
 eval(parse(text=paste(save(file, i, , file=\file, i, .rda\),
 sep=)))


 eval(parse(text = ...)) is necessary sometimes, but usually not. This 
 is one of
 the 'not' cases. Re your question, the backslash is used to escape the 
 quote
 within the quote so that it is rendered properly when parsed/evaluated.


 Jorge, your solution does not work... I've just copy/pasted your code.
 My second great weakness is with the apply() family. So maybe I
 have to
 adjust some part of the code to my needs, but I'm unable to do it.
 i - 1:4
 sapply(i, function(i) {
   x - data.frame(a=(i+1):(i+10), b=LETTERS[(i+1):(i+10)])
 save(x, file = paste(file, i, .rda, sep=))
 } )


 I tried a variation on this solution, and discovered that whenever you 
 load
 a .rda file, the name of the object is the same in all of them. It 
 'works' in the
 sense that the right object is saved to each of the file*.rda; 
 however, when
 loaded, all the objects have the same name x, so in the end it doesn't 
 really
 work.

 Peter figured out that saving the object as a list was the key - that 
 way,
 the name of the object saved is the value of filename (file*) so that 
 when it
 is loaded back in, the object names are distinct.

 A more interesting question than it appeared at first...

 Dennis


 Anyway, everything's now fine!
 Thanks again.
 Ivan


 Le 5/19/2010 20:29, Jorge Ivan Velez a écrit :
  Hi Ivan,
 
  How about this?
 
  i - 1:4
  sapply(i,
  function(i){
  x - data.frame(a=(i+1):(i+10), b=LETTERS[(i+1):(i+10)])
  save(x, file = paste(file, i, .rda, sep=))
  }
  )
 
  HTH,
  Jorge

 Le 5/19/2010 22:20, Peter Ehlers a écrit :
  On 2010-05-19 12:05, Shi, Tao wrote:
  Ivan,
 
  Try this:
 
  eval(parse(text=paste(save(file, i, , file=\file, i,
  .RData\), sep=)))
 
  ...Tao
 
 
  Or just use 'list=' like this:
 
  for (i in 1:4) {
temp - data.frame(a=(i+1):(i+10), b=LETTERS[(i+1):(i+10)])
filename - paste(file, i, sep=)
assign(filename, temp)
save(list=c(filename), file=paste(filename, .rda, sep=))
  }
 
   -Peter Ehlers
 
 
 
  - Original Message 
  From: Ivan Calandraivan.calan...@uni-hamburg.de
 mailto:ivan.calan...@uni-hamburg.de
  To: r-help@r-project.org mailto:r-help@r-project.org
  Sent: Wed, May 19, 2010 7:56:44 AM
  Subject: [R] save in for loop
 
  Dear users,
 
  My problem concerns save() within a for loop.
  Here is my
  code:
 
  for (i in 1:4) {
  temp- data.frame(a=(i+1):(i+10),
  b=LETTERS[(i+1):(i+10)])
  filename- paste(file, i, sep=)
 
  assign(filename, temp)
  save(filename, file=paste(filename, .rda,
  sep=))
  }
 
  As you can see, save() doesn't work as I would like: (1)
  the object saved is called filename (instead of file1,
 file2,
  etc), and
  (2) it of course contains only the name (as character) instead
 of the
  data.frame
 
  How can I fix it?
 
  [snip]
 

 --
 Ivan CALANDRA
 PhD Student
 University of Hamburg
 Biozentrum Grindel und Zoologisches Museum
 Abt. Säugetiere
 Martin-Luther-King-Platz 3
 D-20146 Hamburg, GERMANY
 +49(0)40 42838 6231
 ivan.calan...@uni-hamburg.de mailto:ivan.calan...@uni-hamburg.de

 **
 http://www.for771.uni-bonn.de
 http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php


[[alternative HTML version deleted]]


 __
 R-help@r-project.org mailto:R-help@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.



-- 
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt

[R] save in for loop

2010-05-19 Thread Ivan Calandra

Dear users,

My problem concerns save() within a for loop.
Here is my code:

for (i in 1:4) {
 temp - data.frame(a=(i+1):(i+10), b=LETTERS[(i+1):(i+10)])
 filename - paste(file, i, sep=)
 assign(filename, temp)
 save(filename, file=paste(filename, .rda, sep=))
}

As you can see, save() doesn't work as I would like: (1) the object 
saved is called filename (instead of file1, file2, etc), and (2) 
it of course contains only the name (as character) instead of the data.frame


How can I fix it?

I usually use lists for such cases, but (1) in the real thing, it gets 
complicated with the names and structure (because I want to save lists 
with 3 dimensions instead of simple data.frames, as in this example) and 
(2) I prefer saving each list separately (and I cannot save only one 
element of an object either).


I'm not sure I'm really clear because it's difficult for me to explain 
it, but I hope you'll understand (and let me know what you would help 
you to understand)


Thank you in advance
Ivan

--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calan...@uni-hamburg.de

**
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php

__
R-help@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.


Re: [R] save in for loop

2010-05-19 Thread Shi, Tao
Ivan,

Try this:

eval(parse(text=paste(save(file, i, , file=\file, i, .RData\), sep=)))

...Tao




- Original Message 
 From: Ivan Calandra ivan.calan...@uni-hamburg.de
 To: r-help@r-project.org
 Sent: Wed, May 19, 2010 7:56:44 AM
 Subject: [R] save in for loop
 
 Dear users,

My problem concerns save() within a for loop.
Here is my 
 code:

for (i in 1:4) {
temp - data.frame(a=(i+1):(i+10), 
 b=LETTERS[(i+1):(i+10)])
filename - paste(file, i, sep=)

 assign(filename, temp)
save(filename, file=paste(filename, .rda, 
 sep=))
}

As you can see, save() doesn't work as I would like: (1) 
 the object saved is called filename (instead of file1, file2, etc), and 
 (2) it of course contains only the name (as character) instead of the 
 data.frame

How can I fix it?

I usually use lists for such cases, 
 but (1) in the real thing, it gets complicated with the names and structure 
 (because I want to save lists with 3 dimensions instead of simple 
 data.frames, 
 as in this example) and (2) I prefer saving each list separately (and I 
 cannot 
 save only one element of an object either).

I'm not sure I'm really clear 
 because it's difficult for me to explain it, but I hope you'll understand 
 (and 
 let me know what you would help you to understand)

Thank you in 
 advance
Ivan

-- Ivan CALANDRA
PhD Student
University of 
 Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. 
 Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 
 42838 6231

 href=mailto:ivan.calan...@uni-hamburg.de;ivan.calan...@uni-hamburg.de

**
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php

__

 ymailto=mailto:R-help@r-project.org; 
 href=mailto:R-help@r-project.org;R-help@r-project.org mailing list

 href=https://stat.ethz.ch/mailman/listinfo/r-help; target=_blank 
 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.




__
R-help@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.


Re: [R] save in for loop

2010-05-19 Thread Jorge Ivan Velez
Hi Ivan,

How about this?

i - 1:4
sapply(i,
function(i){
x - data.frame(a=(i+1):(i+10), b=LETTERS[(i+1):(i+10)])
 save(x, file = paste(file, i, .rda, sep=))
}
 )

HTH,
Jorge


On Wed, May 19, 2010 at 10:56 AM, Ivan Calandra  wrote:

 Dear users,

 My problem concerns save() within a for loop.
 Here is my code:

 for (i in 1:4) {
  temp - data.frame(a=(i+1):(i+10), b=LETTERS[(i+1):(i+10)])
  filename - paste(file, i, sep=)
  assign(filename, temp)
  save(filename, file=paste(filename, .rda, sep=))
 }

 As you can see, save() doesn't work as I would like: (1) the object saved
 is called filename (instead of file1, file2, etc), and (2) it of
 course contains only the name (as character) instead of the data.frame

 How can I fix it?

 I usually use lists for such cases, but (1) in the real thing, it gets
 complicated with the names and structure (because I want to save lists with
 3 dimensions instead of simple data.frames, as in this example) and (2) I
 prefer saving each list separately (and I cannot save only one element of an
 object either).

 I'm not sure I'm really clear because it's difficult for me to explain it,
 but I hope you'll understand (and let me know what you would help you to
 understand)

 Thank you in advance
 Ivan

 --
 Ivan CALANDRA
 PhD Student
 University of Hamburg
 Biozentrum Grindel und Zoologisches Museum
 Abt. Säugetiere
 Martin-Luther-King-Platz 3
 D-20146 Hamburg, GERMANY
 +49(0)40 42838 6231
 ivan.calan...@uni-hamburg.de

 **
 http://www.for771.uni-bonn.de
 http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php

 __
 R-help@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.


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] save in for loop

2010-05-19 Thread Peter Ehlers

On 2010-05-19 12:05, Shi, Tao wrote:

Ivan,

Try this:

eval(parse(text=paste(save(file, i, , file=\file, i, .RData\), sep=)))

...Tao



Or just use 'list=' like this:

for (i in 1:4) {
  temp - data.frame(a=(i+1):(i+10), b=LETTERS[(i+1):(i+10)])
  filename - paste(file, i, sep=)
  assign(filename, temp)
  save(list=c(filename), file=paste(filename, .rda, sep=))
}

 -Peter Ehlers




- Original Message 

From: Ivan Calandraivan.calan...@uni-hamburg.de
To: r-help@r-project.org
Sent: Wed, May 19, 2010 7:56:44 AM
Subject: [R] save in for loop

Dear users,


My problem concerns save() within a for loop.
Here is my

code:


for (i in 1:4) {
temp- data.frame(a=(i+1):(i+10),

b=LETTERS[(i+1):(i+10)])

filename- paste(file, i, sep=)


assign(filename, temp)

save(filename, file=paste(filename, .rda,

sep=))

}

As you can see, save() doesn't work as I would like: (1)

the object saved is called filename (instead of file1, file2, etc), and
(2) it of course contains only the name (as character) instead of the
data.frame


How can I fix it?


[snip]

__
R-help@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.