Hi Ed, Here are some different ways of structuring and storing data in R as well as how to access them.
Hope this helps, Josh ######################################################################### set.seed(10) ## Option 1a (closest to what you want?) a1a <- list(river1 = data.frame(flow = 1:10, precipitation = runif(10)), river2 = data.frame(flow = 11:20, precipitation = runif(10))) set.seed(10) ## Option 1b (a slight variant), lists + named vectors a1b <- list(river1 = list(flow = 1:10, precipitation = runif(10)), river2 = list(flow = 11:20, precipitation = runif(10))) set.seed(10) ## Option 2 (a.k.a. 'long' format) a2 <- data.frame(flow = 1:20, precipitation = runif(20), river = factor(rep(1:2, each = 10))) set.seed(10) ## Option 3 (a.k.a. 'wide' format) a3 <- data.frame(r1.flow = 1:10, r2.flow = 11:20, r1.precipitation = runif(10), r2.precipitation = runif(10)) ## compare the str()uctures of all four objects str(a1a) str(a1b) str(a2) str(a3) ## Access "flow" from "river1" in option 1a ## four equivalent options a1a$river1$flow a1a[["river1"]][,"flow"] a1a[[1]][, 1] a1a[[c(1, 1)]] ## Option 1b a1b$river1$flow ## works a1b[[1]][, 1] ## does not work, used vectors not a dataframe a1b[[c(1, 1)]] ## works ## Option 2 a2$flow ## both rivers' flow, useful for plotting a2[, 1] a2[, "flow"] ## for example: require(lattice) xyplot(flow ~ precipitation | river, data = a2) ## Option 3 a3$r1.flow a3[, 1] a3[, "r1.flow"] ################################################################## On Sun, Mar 13, 2011 at 3:42 PM, Eduardo M. A. M.Mendes <emammen...@gmail.com> wrote: > > Hi Joshua > > Many thanks. > > The values of flow can be accessed in a weird way and we can used them for > some calculations. Since I am a newbie as far as using R is concerned I > wonder whether you could tell me how to create a structure in R that looks > like the one I have in matlab (that is, a variable a that contains river1 and > river2 that contains flow and precipitation). > > Cheers > > Ed > > > -----Original Message----- > From: Joshua Wiley [mailto:jwiley.ps...@gmail.com] > Sent: Sunday, March 13, 2011 7:20 PM > To: Eduardo M. A. M.Mendes > Cc: R-help@r-project.org > Subject: Re: [R] readMat - how to retrieve the variables > > On Sun, Mar 13, 2011 at 2:56 PM, Eduardo M. A. M.Mendes > <emammen...@gmail.com> wrote: >> Hi Joshua >> >> Many thanks for the prompt reply. >> >> I have saved a short version of the matlab file and the output on R is >> >>> b=readMat("testr.mat") >>> b >> $a >> , , 1 >> >> [,1] >> river1 List,2 >> river2 List,2 > > It looks like you are dealing with a special series of lists nested within > three dimensional arrays within lists. My suggestion would be to double > check that the matlab file has reasonable data (whatever that means) and try > to double check your use of readmat (do you meet all the requirements for > versions, etc.). That is not a common R structure so the extraction is > similarly uncommon. Perhaps Henrik will be along with more helpful answers. > > Good luck, > > Josh >> >> >> attr(,"header") >> attr(,"header")$description >> [1] "MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: Sun Mar 13 18:51:54 >> 2011 " >> >> attr(,"header")$version >> [1] "5" >> >> attr(,"header")$endian >> [1] "little" >> >> When I issue the command b$a[,,1]$river1[,,1]$flow I see the flow values. >> >> Unfortunately the data is confidential. >> >> Many thanks >> >> Ed >> >> >> -----Original Message----- >> From: Joshua Wiley [mailto:jwiley.ps...@gmail.com] >> Sent: Sunday, March 13, 2011 6:30 PM >> To: Eduardo M. A. M.Mendes >> Cc: R-help@r-project.org >> Subject: Re: [R] readMat - how to retrieve the variables >> >> Hi Ed, >> >> Can you please provide *at least* the R output from running: >> >> str(data) >> >> where "data" is the variable name you stored the results of readMat() in. >> If it is reasonably small and can be sent as plaintext (I do not know >> Matlabs file format off hand), you could send us the actual data so we can >> try to read it in, but at the least str() will let us see how R is storing >> your data and give you some explanation. >> >> Side note, as data() is a function, it might be worthwhile to call your >> actual data something else (say, mydata, dat, etc.). For anyone else >> interested, readMat() is in package "R.matlab". >> >> Cheers, >> >> Josh >> >> On Sun, Mar 13, 2011 at 2:07 PM, Eduardo M. A. M.Mendes >> <emammen...@gmail.com> wrote: >>> Hello >>> >>> I have a matlab MAT file that contains one single variable: a. The >>> structure of a is as follows: >>> >>> a.river1.flow (flow values) >>> a.river1.date_flow (date) >>> a.river1.precip (precipitation values) a.river1.date_precip >>> a.river2.flow a.river2.date_flow a.river2.precip a.river2.date_precip >>> >>> I have used readMat to load the variable a in R, however I have no >>> idea how readMat translates a. I managed to get some values out of >>> data=readMat("matfile.mat") >>> >>> data$a[,,1]$river1[,,1]$flow -> Why do I need [,,1]? Why not >>> data$a$river1$flow? >>> >>> Many thanks >>> >>> Ed >>> >>> ______________________________________________ >>> R-help@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. > > -- > Joshua Wiley > Ph.D. Student, Health Psychology > University of California, Los Angeles > http://www.joshuawiley.com/ > > -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/ ______________________________________________ R-help@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.