Hi,
need a little help:
I want to handle a file with a first column like this:
...
27.10.98
28.10.98
29.10.98
...
In vers. 1.5.1 I can use a statement like:
# load daily
prices
prices<-as.matrix(read.table(paste(path,"prices",date,".txt",sep=""),row.names=1,header=T))
In ves. 1.4.1 R generates an ERROR:
Error in "row.names<-.data.frame"(*tmp*, value = row.names) :
duplicate row.names are not allowed
For a reason too complex to explain I HAVE TO use 1.4.1! Can anyone tell me
how to solve this problem in 1.4.1?
TIA,
Alfred.
--
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Your "problem" is not specific to 1.4.1, or even R. read.table
generates a
data frame, and data frame *must* have unique row names in all versions of R
and Splus (well, almost...).
If you don't need the row and column names, preprocess the file first. If
you know the number of columns in the file, use scan() instead of
read.table. It'll be much faster and memory efficient. For example:
## strip out the first column
cmd <- paste("cut -d' ' -f2- ", path, "prices",
date, ".txt", sep="")
prices <- matrix(scan(pipe(cmd), skip=1), byrow=TRUE, ncol=????)
HTH,
andy
> -----Original Message-----
> From: Herr.Alfred at gmx.net [mailto:Herr.Alfred at gmx.net]
> Sent: Friday, August 23, 2002 3:07 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] row.names - problem with vers. 1.4.1
>
>
> Hi,
>
> need a little help:
>
> I want to handle a file with a first column like this:
> ...
> 27.10.98
> 28.10.98
> 29.10.98
> ...
>
> In vers. 1.5.1 I can use a statement like:
>
> # load daily
> prices
>
prices<-as.matrix(read.table(paste(path,"prices",date,".txt",s
> ep=""),row.names=1,header=T))
>
> In ves. 1.4.1 R generates an ERROR:
> Error in "row.names<-.data.frame"(*tmp*, value = row.names) :
> duplicate row.names are not allowed
>
> For a reason too complex to explain I HAVE TO use 1.4.1! Can
> anyone tell me
> how to solve this problem in 1.4.1?
>
> TIA,
> Alfred.
>
> --
>
>
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-.-.-.-.-.-.-.-
> r-help mailing list -- Read
http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._
------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachments, contains
information of Merck & Co., Inc. (Whitehouse Station, New Jersey, USA) that
may be confidential, proprietary copyrighted and/or legally privileged, and is
intended solely for the use of the individual or entity named in this message.
If you are not the intended recipient, and have received this message in error,
please immediately return this by e-mail and then delete it.
=============================================================================
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
This should work:
prices.raw<-read.table(paste(path,"prices",date,".txt",sep=""),row.names=NULL,header=T)
prices <- as.matrix(prices.raw[,-1])
dimnames(prices)[[1]] <- as.chararacter(prices.raw[,1])
Your problem comes because you are telling R to use the first column of the
table as row names, but as R is replying to you, it doesn't like that
because they're not unique. So the simple solution is to let R generate
it's own row names and treat the row names in the file as the first column
of data. Then convert the rest of the data frame to a matrix, and finally,
use the first column of the data frame as the row names of the
matrix. (Matrices can have non-unique dimnames).
Are you sure you can use read.table in R1.5.1 to read a file with duplicate
row names? I can't:
>
sink("clipboard");print(matrix(1:6,ncol=2,dimnames=list(c("A","B","A"),c("x","y"))));
sink()
> readLines("clipboard")
[1] " x y" "A 1 4" "B 2 5" "A 3 6"
> prices.raw <- read.table("clipboard", row.names=1)
Error in "row.names<-.data.frame"(*tmp*, value = row.names) :
duplicate row.names are not allowed
> prices.raw <- read.table("clipboard", row.names=NULL)
> prices.raw
row.names x y
1 A 1 4
2 B 2 5
3 A 3 6
> prices <- as.matrix(prices.raw[,-1])
> dimnames(prices)[[1]] <- as.character(prices.raw[,1])
> prices
x y
A 1 4
B 2 5
A 3 6
> version
_
platform i386-pc-mingw32
arch i386
os mingw32
system i386, mingw32
status
major 1
minor 5.1
year 2002
month 06
day 17
language R
hope this helps,
-- Tony Plate
At 09:07 AM 8/23/2002 +0200, Herr.Alfred at gmx.net
wrote:>Hi,
>
>need a little help:
>
>I want to handle a file with a first column like this:
>...
>27.10.98
>28.10.98
>29.10.98
>...
>
>In vers. 1.5.1 I can use a statement like:
>
># load daily
>prices
>prices<-as.matrix(read.table(paste(path,"prices",date,".txt",sep=""),row.names=1,header=T))
>
>
>In ves. 1.4.1 R generates an ERROR:
>Error in "row.names<-.data.frame"(*tmp*, value = row.names) :
> duplicate row.names are not allowed
>
>For a reason too complex to explain I HAVE TO use 1.4.1! Can anyone tell me
>how to solve this problem in 1.4.1?
>
>TIA,
>Alfred.
>
>--
>
>
>
>-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
>r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
>Send "info", "help", or "[un]subscribe"
>(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
>_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._