Greetings, When I try to use the write.table command to save a matrix as a file and then open the file with read.table, if I try to take the mean of the entire matrix, instead each column of the matrix has its mean calculated. I have copied and pasted an example of my code below. When I try to make the header false with the read.table command, I am given an error message. I would appreciate any guidance for how to average the entire matrix instead of just the columns. Thanks z=matrix(1:20,4,5) write.table(z, file="exercise.csv",sep=",",col.names=NA,qmethod="double") j=read.table("exercise.csv",header=T,sep=",",row.names=1) mean(j) V1 V2 V3 V4 V5 2.5 6.5 10.5 14.5 18.5 Warning message: mean(<data.frame>) is deprecated. Use colMeans() or sapply(*, mean) instead. [[alternative HTML version deleted]]
R. Michael Weylandt <michael.weylandt@gmail.com>
2012-Aug-28 03:04 UTC
[R] write.table and read.table commands
It's because read.table returns a data frame, not a matrix. You can coerce to a matrix with as.matrix() but you might loose information if your variables are of different classes. Michael On Aug 27, 2012, at 7:07 PM, Cheryl Johnson <johnson.cheryl625 at gmail.com> wrote:> Greetings, > > When I try to use the write.table command to save a matrix as a file and > then open the file with read.table, if I try to take the mean of the entire > matrix, instead each column of the matrix has its mean calculated. I have > copied and pasted an example of my code below. When I try to make the > header false with the read.table command, I am given an error message. I > would appreciate any guidance for how to average the entire matrix instead > of just the columns. > > Thanks > > z=matrix(1:20,4,5) > write.table(z, file="exercise.csv",sep=",",col.names=NA,qmethod="double") > j=read.table("exercise.csv",header=T,sep=",",row.names=1) > mean(j) > > V1 V2 V3 V4 V5 > 2.5 6.5 10.5 14.5 18.5 > Warning message: > mean(<data.frame>) is deprecated. > Use colMeans() or sapply(*, mean) instead. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.