[R] Suprising behavior of paste or cat?

Peter Ehlers ehlers at ucalgary.ca
Thu Feb 11 07:38:12 CET 2010


Russell Pierce wrote:
> I may be making a simple error, but I've looked at the str() of the
> resulting objects and I can't see any obvious reason I'm having the
> problem I am having, so I am reaching out to the R-help group.  I am
> generating a string in my code.  When I make a slight modification
> (add a comma at the end using my "lastcomma" function), I can no
> longer successfully write that string to a file.  Specifically, the
> resulting file contains only the "ⰱ" character.  This occurs in:
> R version 2.10.0 (2009-10-26) & R version 2.10.1 (2009-12-14)
> i386-pc-mingw32
> locale:
> [1] LC_COLLATE=English_United States.1252
> [2] LC_CTYPE=English_United States.1252
> [3] LC_MONETARY=English_United States.1252
> [4] LC_NUMERIC=C
> [5] LC_TIME=English_United States.1252
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
> but not in...
> R version 2.7.1 (2008-06-23)
> x86_64-pc-linux-gnu
> locale:
> LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=C;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
> Sample code:
> h.long <- 150
> task <- c(rep(1,h.long),rep(2,h.long))
> ord <- sample(1:length(task))
> task <- task[ord]
> taskout <- paste(task,collapse=",")
> write(file="please.txt",taskout)
> lastcomma <- function(x) {return(paste(x,",",collapse="",sep=""))}
> res <- lastcomma(taskout)
> write(file="fail.txt",res)
> cat(file="catfail.txt",res)
> 
> Any ideas as to how to avoid this problem would be appriciated as well
> as suggestions as to whether this is expected behavior, or whether it
> ought to be reported as a bug.

I don't see the problem in R version 2.10.1 Patched (2010-01-05 r50896)
nor in R 2.11.0 Pre-release (Windows Vista).

  -Peter Ehlers

> 
> Best,
> 
> Russell Pierce
> 

-- 
Peter Ehlers
University of Calgary



More information about the R-help mailing list