[R] RE:
Guido Masarotto
guido at hal.stat.unipd.it
Wed Nov 10 09:07:49 CET 1999
On Tue, Nov 09, 1999 at 04:34:33PM +0100, Peter Dalgaard BSA wrote:
> Martyn Plummer <plummer at iarc.fr> writes:
>
> > I usually do this by saving the spreadsheet in comma-separated variable
> > (csv) format, and then read it into R with this command
> >
> > foo.dat <- read.table("foo.csv", header=TRUE, sep=",")
>
> ...which reminds me that my students keep complaining that Excel does
> CSV files with comma as decimal point and semicolon as separators if
> Windows is localised to Danish (and I suppose also German). At least
> it is better than some other programs that would use comma for both
> items(!) but I suppose we need to cater for that kind of format along
> with the other CSV specialities. (BTW is there any way of
> distinguishing between 2 integer columns with comma as separator and 1
> column of decimal numbers with comma as decimal point??)
Same in Italy.
However, Peter, problem is that, if I remember well, you teach
medical students. Some times ago, a group of students in Statistics
here didn't complain and ended up with the following wrapper
for read.table. Maybe not efficient and without any error
checking but handy.
guido
> read.comma.is.point
function(...,type=rep("n",n.var),no.quote=TRUE) {
a <- read.table(...)
names <- colnames(a)
n.var <- length(names)
for (i in 1:n.var)
if (type[i]=="n") {
a[,i] <- as.numeric(gsub(",",".",as.character(a[,i])))
} else {
if (no.quote)
a[,i] <- gsub("^ *\"","",gsub("\" *$","",as.character(a[,i])))
}
names <- gsub("^ *\"","",names)
names <- gsub("\" *$","",names)
colnames(a) <- names
a
}
> system("cat Rachmaninoff");cat("\n")
"FirstVar";"SecondVar";"ThirdVar"
1,2; "one" ; 1,4
2,3 ; "two"; 2
> read.table("Rachmaninoff",header=T,sep=";")->a
> a
\"FirstVar\"\"SecondVar\"\"ThirdVar\"
1 1,2 \"one\" 1,4
2 2,3 \"two\" 2
> attach(a)
> FirstVar
Error: Object "FirstVar" not found
> sum(a[,1])
Error in Summary.factor(..., na.rm = na.rm) : "sum" not meaningful for factors
> detach(a)
> read.comma.is.point("Rachmaninoff",header=T,sep=";",type=c("n","c","n"))->a
> a
FirstVar SecondVar ThirdVar
1 1.2 one 1.4
2 2.3 two 2.0
> attach(a)
> FirstVar
[1] 1.2 2.3
> sum(FirstVar)
[1] 3.5
>
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list