[R] Problems with read.table

Guido Masarotto guido at hal.stat.unipd.it
Tue Nov 9 20:08:14 CET 1999


On Tue, Nov 09, 1999 at 11:31:27AM +0000, Heberto Ghezzo wrote:
> Hi I am using R65.1 in Windows 95
> I have a CSV file from Excell
> > fvcpp
> [1] "99.28 " "86.97 " "81.12 " "98.12 " "90.50 "
> > fvcpp<-as.numeric(fvcpp)
> > fvcpp
> [1] 99.28 86.97 81.12 98.12 90.50
> > mean(fvcpp)
> [1] 91.198
> >
> 
> -- this is obviously not the way to do it, for each variable
> change it into numeric.
>  

  If all variables are numeric, as part omitted of your message seems
  to point to, you can transform them in one step:

> system("cat Chopin");cat("\n")
1 , 2
3 , 4
> a <- read.table("Chopin",sep=",")
> a
  V1 V2
1 1   2
2 3   4
> attach(a)
> mean(V1)
Error: "sum" not meaningful for factors

But, 
> a <- data.frame(apply(read.table("Chopin",sep=","),2,as.numeric))
> attach(a)
> mean(V1)
[1] 2


On the other hand, if you have a mix of factor e/o character variables 
and numeric variables, things are a more complicated (and perhaps 
does exist a better solution than the following one):

> system("cat Bartok");cat("\n")
1 , two , 3
4 , five, 6
> a <- read.table("Bartok",sep=",")
> var.numeric <- c("V1","V3")
> index <- match(var.numeric,colnames(a))
> names <- colnames(a)
> a <- data.frame(apply(a[,index],2,as.numeric),a[,-index])
> colnames(a) <- c(names[index],names[-index])
> a
  V1 V3    V2
1  1  3  two 
2  4  6  five
> attach(a)
> mean(V1)
[1] 2.5

Hoping this help,
guido
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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