[R] Newbie: Subsets of data frame

Martin Maechler maechler at stat.math.ethz.ch
Mon Sep 23 17:20:43 CEST 2002


>>>>> "Morten" == Morten Sickel <Morten.Sickel at nrpa.no>
>>>>>     on Mon, 23 Sep 2002 16:58:43 +0200 writes:

    Morten> I feel a bit embarassed by this, but I have not
    Morten> managed to find out how: I have a data frame
    Morten> containing measurements over time for a number of
    Morten> different locations:

    >> str(milk)
    Morten> `data.frame':   845 obs. of  3 variables:
    Morten> $ date    : num  1987 1987 1987 1987 1987 ...
    Morten> $ value   : num  5 1 2 5 1 1 2 3 2 4 ...
    Morten> $ location: Factor w/ 36 levels "Alta","Andøy",..: 1 1 1 1 1 1 1 1 1 1 ...

    Morten> I assume there is some simpe way of extracting
    Morten> subsets of the data frame for analysing the time
    Morten> series for just one of the location, but how? I have
    Morten> tried using various variants of extract, [] and
    Morten> subset, but obiously, I have missed something, for I
    Morten> have not managed to get out what I want.

  milkA <- milk[ milk$location == "Alta" , ]

or a bit more robustly,

  milkA <- subset(milk, location == "Alta")


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list