Skip to content

Output to connections

8 messages · Adelchi Azzalini, Henrik Bengtsson, Philippe Hupé +3 more

#
In the document "R Data Import/Export", section "Output to connections",
there is the following portion of code:

  ## convert decimal point to comma in output, using a pipe (Unix)
  zz <- pipe(paste("sed s/\\./,/ >", "outfile"), "w")
  cat(format(round(rnorm(100), 4)), sep = "\n", file = zz)
  close(zz)
  ## now look at the output file:
  file.show(outfile, delete.file = TRUE)

Surely the last fine must be 
  file.show("outfile", delete.file = TRUE)
However this is not the problem, but the fact that I get something like
,1.6861
,0.1934
,0.5640
,0.5741
,0.1920
,0.1898
,1.4788
,0.1706
,0.9953
<..snipped..>

If I run from R:
  zz <- file("outfile", "w")
  cat(format(round(rnorm(10), 4)), sep = "\n", file = zz)
  close(zz)
and then from outside R:
  sed s/\\./,/ outfile 
then I get it right (of course), something like
-1,3612
-0,9772
 0,1524
 2,4046
 0,4741
 0,6659
-0,8277
 0,5071
 0,7190
 0,4088

This is fine, but it would be good to have it working in first form.
 
environment: R 1.7.0 on Debian linux.

regards,

Adelchi Azzalini
#
In R '\' has to be escaped, i.e. '\\' which means '\\' has to be '\\\\'
(this was probably there before the help page was generated!?)

The following works

  ## convert decimal point to comma in output, using a pipe (Unix)
  zz <- pipe(paste("sed s/\\\\./,/ >", "outfile"), "w")
  cat(format(round(rnorm(100), 4)), sep = "\n", file = zz)
  close(zz)
  ## now look at the output file:
  file.show("outfile", delete.file = TRUE)

Henrik Bengtsson
http://azzalini.stat.unipd.it/

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
#
On Tuesday 20 May 2003 13:03, Henrik Bengtsson wrote:
Yes, it works, thanks

Adelchi
#
Hello,

I would like to extract unique elements of a variable which belongs to a 
list

liste <- list(V1=c(1,2,3,5,2), V2=c(1,2,3,4,5))
var <- "V1"
uni <- unique(liste[var]) #does not work

I know that liste[var]$V1 works but for my problem, the label variable 
"V1" is only know through the var variable.

I can I do

Thanks in advance.
#
On Tue, 20 May 2003, Adelchi Azzalini wrote:

            
Um, that's not the problem.  If this is a Unix-alike, the shell invoked by
popen also (probably, depending on the shell) needs "\" escaped. So each
of

the help processing
R strings
the Unix shell (probably)

need \ doubled (probably), and it is the last one I forgot.

  
    
#
Philippe Hup? wrote:
Need to use "[[" here. liste[var] is still a list. liste[[var]] is a 
vector. I would avoid using "var" as a variable name since it is also 
the function for computing the variance.
lapply(liste, unique) returns a list of length(liste) containing the 
unique values of each element in the list.

Regards,
Sundar
#
You meant [[var]] not [var].  liste[var] is a one-element list.
On Tue, 20 May 2003, Philippe Hup? wrote: