[R] Extracting rows from a dataset

Rui Barradas ruipbarradas at sapo.pt
Thu May 31 00:20:58 CEST 2012


Hello,

Try the following.

ix <- apply(dat, 1, function(x) x[1] == 1 && sum(diff(x) != 0) > 1)
dat[ix, ]


Hope this helps,

Rui Barradas

Em 30-05-2012 22:45, arun escreveu:
> Dear R help,
>
> I have a dataset with 1's and 0's.  Here, each row is the observation for an insect.  If the animal is present in light area at a particular time, response is 1 and if it is present in dark area, the response is 0.  I would like to do some formating on the data for analysis.  To be specific, I need the rows where the animal starts in the light (1), went to dark (0), and came back to light (1).  If the animal comes back multiple times into light, this should also be included.   
>
>
> dat<-read.table(text="
> min_0 min_1 min_2 min_3 min_4 min_5 min_6 min_7 min_8 min_9 min_10 min_11 min_12 min_13 min_14 min_15
> 1     1     1     1   1    0   0    0    0    0     1     1     1    1      1    1
> 1     1     1     1   1    1   1    1    1    1     1     1     1    1      1    1
> 1     0     0     0   0    1   1    0    0    0     1     1     1    1      1    1
> 1     1     1     1   1    1   1    1    1    1     0     0     0    0      0    0
> 1     1     1     1   1    1   0    0    0    0     0     0     0    0      0    0
> 1     0     1     0   0    1    0   1    1     1    0     0     0    0      1     1
> ",sep="",header=T)                 
>
>
> The output I would expect is:
>
> min_0 min_1 min_2 min_3 min_4 min_5 min_6 min_7 min_8 min_9 min_10 min_11 min_12 min_13 min_14 min_15
> 1     1     1     1   1    0   0    0    0    0     1     1     1    1      1    1
> 1     0     0     0   0    1   1    0    0    0     1     1     1    1      1    1
> 1     0     1     0   0    1    0   1    1     1    0     0     0    0      1     1
>
> Any ideas on how to proceed?
>
> Thanking you,
>
> A.K.
>
> ______________________________________________
> 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.



More information about the R-help mailing list