Matthijs Daelman
2012-Dec-22 10:58 UTC
[R] Does R have an equivalent for Matlab's cell array?
Hi I have a time series of measurements: every 10 min. a value was logged. The data look like: 20100914 08:01 3.74 20100914 08:11 3.74 20100914 08:21 3.71 20100914 08:31 4.39 20100914 08:41 3.74 This data spans several months. I would like to group the data per day. In Matlab it is fairly easy to obtain a cell array, of which the first column contains the date of each day, and the second column contains a vector, that in its turn contains all values for the corresponding day. That looks like 14-9-2010 [3.74 3.74 3.71 4.39 3.74...] 15-9-2010 [...] and so on. Is it possible to create a similar data structure in in R? I was thinking of a data frame, similar to the Matlab's cell array, but it doesn't look like data frames can contain vectors. Something like a two dimensional list would do the trick, I believe, but does that exist? Thanks Kind regards Matthijs Daelman
Jeff Newmiller
2012-Dec-22 19:27 UTC
[R] Does R have an equivalent for Matlab's cell array?
Data frames are lists of vectors. You can have the vectors in a data frame be of mode list, if you wish. It would be more conventional to simply make the list and not put it into the data frame, since columns of mode list give write.table fits. I prefer to simply keep the first form of your data and augment it with a Day column. Then if I want information on a per-day basis I use aggregate from base or ddply from the plyr package. Although I prefer your first form (long format) there are many cases where it makes sense to put groups of data values in separate columns (wide format). You can use the reshape2 package for transforming back and forth. You may also want to check out the Time Series task view on CRAN. Most of those classes work with data in array representation for high speed. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Matthijs Daelman <matthijs.daelman at gmail.com> wrote:>Hi > >I have a time series of measurements: every 10 min. a value was logged. > >The data look like: > >20100914 08:01 3.74 >20100914 08:11 3.74 >20100914 08:21 3.71 >20100914 08:31 4.39 >20100914 08:41 3.74 > >This data spans several months. > >I would like to group the data per day. In Matlab it is fairly easy to >obtain a cell array, of which the first column contains the date of >each >day, and the second column contains a vector, that in its turn contains > >all values for the corresponding day. > >That looks like > >14-9-2010 [3.74 3.74 3.71 4.39 3.74...] >15-9-2010 [...] >and so on. > >Is it possible to create a similar data structure in in R? I was >thinking of a data frame, similar to the Matlab's cell array, but it >doesn't look like data frames can contain vectors. Something like a two > >dimensional list would do the trick, I believe, but does that exist? > >Thanks > >Kind regards > >Matthijs Daelman > >______________________________________________ >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.
David Winsemius
2012-Dec-22 19:28 UTC
[R] Does R have an equivalent for Matlab's cell array?
On Dec 22, 2012, at 2:58 AM, Matthijs Daelman wrote:> Hi > > I have a time series of measurements: every 10 min. a value was logged. > > The data look like: > > 20100914 08:01 3.74 > 20100914 08:11 3.74 > 20100914 08:21 3.71 > 20100914 08:31 4.39 > 20100914 08:41 3.74 > > This data spans several months. > > I would like to group the data per day. In Matlab it is fairly easy to obtain a cell array, of which the first column contains the date of each day, and the second column contains a vector, that in its turn contains all values for the corresponding day. > > That looks like > > 14-9-2010 [3.74 3.74 3.71 4.39 3.74...] > 15-9-2010 [...] > and so on. > > Is it possible to create a similar data structure in in R? I was thinking of a data frame, similar to the Matlab's cell array, but it doesn't look like data frames can contain vectors.That's a surprising conclusion. Vectors are typical components of dataframes.> Something like a two dimensional list would do the trick, I believe, but does that exist?A dataframe _is_ a two-dimensional list. You should probably explain what you are trying to do, since there is very possibly a better way of structuring this problem without recasting the data into a "wide format". Most of the data processing in R is handled very well without such a strategy. If you wanted to proceed despite my misgivings, then there are several functions such as `split`, `tapply` and `by` that could allow you to segregate such data. It's best to use dput to present the data since console output is often ambiguous as to the underlying data structures. From what you have offered it's not clear that you have yet done the data input processing. -- David Winsemius Alameda, CA, USA
Hello, Something like this? dat <- read.table(text=" 20100914 08:01 3.74 20100914 08:11 3.74 20100914 08:21 3.71 20100914 08:31 4.39 20100914 08:41 3.74 ") dat split(dat$V3, dat$V1) This gives you a list where each element is a (1-dimensional) vector, and each of those vectors can have any length, not necessarily the same. Hope this helps, Rui Barradas Em 22-12-2012 10:58, Matthijs Daelman escreveu:> Hi > > I have a time series of measurements: every 10 min. a value was logged. > > The data look like: > > 20100914 08:01 3.74 > 20100914 08:11 3.74 > 20100914 08:21 3.71 > 20100914 08:31 4.39 > 20100914 08:41 3.74 > > This data spans several months. > > I would like to group the data per day. In Matlab it is fairly easy to > obtain a cell array, of which the first column contains the date of > each day, and the second column contains a vector, that in its turn > contains all values for the corresponding day. > > That looks like > > 14-9-2010 [3.74 3.74 3.71 4.39 3.74...] > 15-9-2010 [...] > and so on. > > Is it possible to create a similar data structure in in R? I was > thinking of a data frame, similar to the Matlab's cell array, but it > doesn't look like data frames can contain vectors. Something like a > two dimensional list would do the trick, I believe, but does that exist? > > Thanks > > Kind regards > > Matthijs Daelman > > ______________________________________________ > 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.
HI, May be this helps: dat1 <- read.table(text=" 20100914 08:01,??? 3.74 20100914 08:11,??? 3.74 20100914 08:21,??? 3.71 20100914 08:31,??? 4.39 20100914 08:41,??? 3.74 20100915 08:01,??? 3.64 20100915 08:11,??? 3.54 20100915 08:21,??? 3.61 20100915 08:31,??? 4.49 20100915 08:41,??? 3.84 ", sep=",",header=FALSE,stringsAsFactors=FALSE) tapply(dat1$V2,list(as.Date(dat1$V1,format="%Y%m%d %H:%M")),function(x) x) #$`2010-09-14` #[1] 3.74 3.74 3.71 4.39 3.74 # #$`2010-09-15` #[1] 3.64 3.54 3.61 4.49 3.84 A.K. ----- Original Message ----- From: Matthijs Daelman <matthijs.daelman at gmail.com> To: r-help at r-project.org Cc: Sent: Saturday, December 22, 2012 5:58 AM Subject: [R] Does R have an equivalent for Matlab's cell array? Hi I have a time series of measurements: every 10 min. a value was logged. The data look like: 20100914 08:01? ? 3.74 20100914 08:11? ? 3.74 20100914 08:21? ? 3.71 20100914 08:31? ? 4.39 20100914 08:41? ? 3.74 This data spans several months. I would like to group the data per day. In Matlab it is fairly easy to obtain a cell array, of which the first column contains the date of each day, and the second column contains a vector, that in its turn contains all values for the corresponding day. That looks like 14-9-2010? ? [3.74 3.74 3.71 4.39 3.74...] 15-9-2010? ? [...] and so on. Is it possible to create a similar data structure in in R? I was thinking of a data frame, similar to the Matlab's cell array, but it doesn't look like data frames can contain vectors. Something like a two dimensional list would do the trick, I believe, but does that exist? Thanks Kind regards Matthijs Daelman ______________________________________________ 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.
Apparently Analagous Threads
- How to plot multiple time series with different time base in same plot?
- looking for equivalent of matlab's medfilt1 function
- ggplot2: two time series with different dates in a single plot
- R equivalent to MATLAB's "whos" Command?
- R equivalent to Matlab's Bayes net toolbox