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.
Exactly what errors are you getting? What is the 'str(a)' so we have an idea of the data you are processing. Why don't you use save/load so that the data is saved in the original format. Have you checked the structure of the data before/after the write.table/read.table? Also take a look at what is being returned with your 'mean(a[rep,])'; this would appear to be multivalued depending on what your dataframe is: e.g.,> x <- data.frame(a=1:10, b=1:10, c=letters[1:10]) > mean(x)a b c 5.5 5.5 NA Warning message: In mean.default(X[[3L]], ...) : argument is not numeric or logical: returning NA So there is more information that you have to provide; also try to look at the structure of all your objects to see if they are what you think they should be. On Wed, Dec 2, 2009 at 6:36 PM, aegea <gcheer3 at gmail.com> 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
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