[R] calculation problem when export and import data

Don MacQueen macq at llnl.gov
Thu Dec 3 06:55:23 CET 2009


After

   a=read.table("test.txt")

do this:

    a <- as.matrix(a)

read.table() gives you a data frame. You are assuming it is a matrix, 
so change it into a matrix.

-Don

At 3:36 PM -0800 12/2/09, aegea wrote:
>Hello,
>
>I have a question on export and import data. Thank you for any suggestions.
>
>data 'simul' is generated as follows:
>N     <- 20                   
>n     <- N/2
>nsets <- 10
>simul <- matrix(0,nsets,N)
>th    <- c(0,1, 1)
>for(i in 1:nsets){
>     simul[i,] <- rnorm(N,mean= rep(th[1:2],N/2),sd=th[3])
>}
>
>I exported data as follows:
>write.table(simul, file="D:\\test.txt", row.names=F, col.names=F)
>
>When I want to use this data, I imported as follows:
>a=read.table("D:\\test.txt")
>
>So far, it works well. When I deal with data, I need use each row to do
>calculations:
>
>for(rep in 1:nsets){
>y   <- a[rep,]
>b<-c(mean(y)+3, mean(y)-4) # cannot calculate mean(y), the mean of this row
>m<-sd(y)   # also cannot calculate sd(y)
>}
>
>I need a lot of calculation based on y, but after I imported data, R comes
>error on it.
>
>Could you please give me some suggestions?
>
>
>--
>View this message in context: 
>http://*n4.nabble.com/calculation-problem-when-export-and-import-data-tp947250p947250.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>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.


-- 
---------------------------------
Don MacQueen
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062
macq at llnl.gov




More information about the R-help mailing list