Skip to content

write.table file="file.txt" help

8 messages · Kristin Kay Nicodemus, Andy Bunn, Sundar Dorai-Raj +4 more

#
Hi all,

I have a R script that creates several input files for an analysis 
program.  It loops through the matrix read into R and picks out 
submatrices and then creates a separate output file for each 
submatrix.  The loop works great, but I am having trouble getting all 
the separate output files written.

The line I have is:

write.table(ch1d, file="C:/WINDOWS/Desktop/SNPs/haplo.txt", 
row.names=F, col.names=F, append=F, quote=F)

Which works just fine if I just wanted to create a single file from the 
loop.  However, I need to somehow get it to change the name of the 
output file ("haplo.txt") each time it goes through the loop so it 
doesn't overwrite each time.  In perl, I'd create $n=1 and increment up 
each loop, and call the file something like "haplo.txt.$n"  

I tried to do something like that but R doesn't recognize the variable 
that would be $n in perl (because it's part of the quoted name of the 
output file).  Adding it after the ending " just gave me an error, as I 
thought it would.

I also tried to use system(copy ...) to change the name of the file in 
dos, but my knowledge of dos is abysmal, so I was unable to do it.

Any ideas on how to go about doing this would be most appreciated!

Thanks in advance,
KK Nicodemus
#
Look at ?paste

for (j in 1:10) { 
   write.table(j, file=paste("haplo.txt", j, sep="."), 
               row.names=F, col.names=F, append=F, quote=F)
}

BTW, there have been many similar posts like this in the past. They are
easily found using the search function at 
http://cran.r-project.org/search.html 

HTH, Andy
#
Kristin Kay Nicodemus wrote:

            
Use paste().

for(i in 1:n) {
   file <- paste("C:/WINDOWS/Desktop/SNPs/haplo", i, "txt", sep = ".")
   cat("Writing data to", file, "\n")
   write.table(ch1d, file=file,
             row.names=F, col.names=F, append=F, quote=F)
}

-sundar
#
I have problems running R with screen. For those who don't know,
screen is a unix tool that is quite handy if you want to leave
a process (that outputs to tty) running when you logout, and
then recover the session later on.

It works like this:
1) run 'screen': you get a normal prompt as if you were in a normal
shell.
2) run whatever command you like
3) press 'C-a d' to detach the session. Now you can logoff if you like
4) when you want to recover the session type 'screen -r'

The problem is that R seems to catch the 'C-a' signal, and nothing happens.
Since there is no way to detach the screen, there is no use to it either.

Is there a way around this problem?

Giampiero
_________________________________________________________
Giampiero Salvi, M.Sc.          www.speech.kth.se/~giampi
Speech, Music and Hearing       Tel:      +46-8-790 75 62
Royal Institute of Technology   Fax:      +46-8-790 78 54
Drottning Kristinasv. 31,  SE-100 44,  Stockholm,  Sweden
#
Hi!

No such problem here. SuSE 9.0, R-1.8.1, screen 4.0.1
Everything works as intended.

If C-a d doesn't work, you can always try screen -D as described in 
the man page.

Hej da

detlef

On Tue, 27 Jan 2004 18:02:53 +0100 (CET)
Giampiero Salvi <giampi at speech.kth.se> wrote:

            

  
    
#
Hej Giampiero!

Odd. Using Screen version 3.09.11 and R-1.8.0 on a
Debian system, R does not trap C-a. I can fire up R in a screen, create
a new screen with C-a c and even detach/reattach the screen session.
The terminal emulator is xterm.

Perhaps you are using a terminal that traps the C-a? (just a wild 
guess..).

Mvh,

/Fredrik Karlsson
On Tue, Jan 27, 2004 at 06:02:53PM +0100, Giampiero Salvi wrote:
#
C-a is not a `signal' but a normal key sequence, and one used by 
readline.  However,

screen R
ctrl-a d
[detached]
screen -r

works for me apart from not having the usual meaning of crtl-a.  I don't
think R is doing anything special, but some versions of readline could 
conceivably be.

I used RH8.0 Linux for this test.  I would suggest trying R --no-readline
in case your version of readline is setting the terminal in a way that 
conflicts with your version of `screen'.
On Tue, 27 Jan 2004, Giampiero Salvi wrote:

            

  
    
#
Hi,
On Wed, 28 Jan 2004, Prof Brian Ripley wrote:

            
Sorry for the wrong terminology.

I tried it again and it works now (even with readline, on RH9.0).
The only way I can explain the problem I got yesterday is that the
computer reacted very slowly (possibly for high load). Strangely
enough, I had tried it on four different computers with the same
result...

Thank you for your help.
Giampiero