[R] Subset any value and blanks

jim holtman jholtman at gmail.com
Thu Sep 20 02:09:13 CEST 2007


You can use 'split' to create a list of dataframes and then operate on them:

  Day Month Year Time Hits Misses F.type
1  01    01 1999 0600  120     80   0600
2  01    01 1999 1015  300     10
3  01    01 1999 1216  250     50   1216
4  01    01 1999 1649  380      0
5  01    01 1999 2132 1100     25   2132
> str(x)
'data.frame':   5 obs. of  7 variables:
 $ Day   : chr  "01" "01" "01" "01" ...
 $ Month : chr  "01" "01" "01" "01" ...
 $ Year  : chr  "1999" "1999" "1999" "1999" ...
 $ Time  : chr  "0600" "1015" "1216" "1649" ...
 $ Hits  : chr  "120" "300" "250" "380" ...
 $ Misses: chr  "80" "10" "50" "0" ...
 $ F.type: chr  "0600" "" "1216" "" ...
> x.split <- split(x, x$F.type == "")
> x.split
$`FALSE`
  Day Month Year Time Hits Misses F.type
1  01    01 1999 0600  120     80   0600
3  01    01 1999 1216  250     50   1216
5  01    01 1999 2132 1100     25   2132

$`TRUE`
  Day Month Year Time Hits Misses F.type
2  01    01 1999 1015  300     10
4  01    01 1999 1649  380      0
> x.missing <- x.split$"TRUE"
> x.missing
  Day Month Year Time Hits Misses F.type
2  01    01 1999 1015  300     10
4  01    01 1999 1649  380      0


On 9/19/07, Spilak,Jacqueline [Edm] <Jacqueline.Spilak at ec.gc.ca> wrote:
> Hi everyone
> I need help with subseting a data set.  In my dataset there is a
> specific row that will either have a value or be blank.  I would like to
> subset (or split) the dataset into one dataset with the row that has the
> value and another dataset that has just the blanks.  Now the thing is
> that the row that has the value in it, the value is not always the same,
> it is a time stamp of when something happens.
> Example of Dataset
> Day     Month   Year    Time    Hits    Misses    F-type
> 01      01      1999    0600    120     80        0600
> 01      01      1999    1015    300     10
> 01      01      1999    1216    250     50        1216
> 01      01      1999    1649    380     0
> 01      01      1999    2132    1100    25        2132
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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 you are trying to solve?



More information about the R-help mailing list