[R] Suprising behavior of paste or cat?

Russell Pierce rpier001 at ucr.edu
Thu Feb 11 06:20:48 CET 2010


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.

Best,

Russell Pierce



More information about the R-help mailing list