[R] Extracting rows from a dataset
arun
smartpink111 at yahoo.com
Thu May 31 00:28:11 CEST 2012
Thanks Jorge,
It worked.
A.K.
________________________________
From: Jorge I Velez <jorgeivanvelez at gmail.com>
To: arun <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Wednesday, May 30, 2012 6:11 PM
Subject: Re: [R] Extracting rows from a dataset
Hi A.K.,
Thank you for the example.
Here is one way:
dat[apply(dat, 1, function(x) x[1] == 1 & length(rle(x)$values) > 2), ]
See ?rle and ?apply for more details.
HTH,
Jorge.-
On Wed, May 30, 2012 at 5:45 PM, arun <> wrote:
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