Quantcast

Select a set of rows based on a condition

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Select a set of rows based on a condition

RaoulD
Hi,

I need help with selecting a set of rows from a column in a dataset, that matches a string criteria - start and end. The dataset is :

<variable Name>   <Value>
   List|Index           10
   ABC                  20
   DEF                   10
   GHI                   50
   JKL                    40
   MNO                  20
   PQR                   10
   
Start="DEF"
End = "MNO"

Ideally, I would like to select the data from DEF to MNO into a new data set. My logic is
1) get the row number for DEF
2) get the row number for MNO
3) subset the rows from DEF to MNO

Can someone please help me with the R code for this as am a little rusty. Please let me know if you have questions.

Thanks in advance!
Raoul
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Select a set of rows based on a condition

Michael Weylandt
If you know the index letters are unique.

df <- data.frame(indx = LETTERS[1:7], levels = sample(7))

ind1 <- which(df$indx == "A")
ind2 <- which(df$indx == "B")

df[ind1:ind2, ]

Michael

On Wed, Dec 7, 2011 at 6:23 AM, RaoulD <[hidden email]> wrote:

> Hi,
>
> I need help with selecting a set of rows from a column in a dataset, that
> matches a string criteria - start and end. The dataset is :
>
> <variable Name>   <Value>
>   List|Index           10
>   ABC                  20
>   DEF                   10
>   GHI                   50
>   JKL                    40
>   MNO                  20
>   PQR                   10
>
> Start="DEF"
> End = "MNO"
>
> Ideally, I would like to select the data from DEF to MNO into a new data
> set. My logic is
> 1) get the row number for DEF
> 2) get the row number for MNO
> 3) subset the rows from DEF to MNO
>
> Can someone please help me with the R code for this as am a little rusty.
> Please let me know if you have questions.
>
> Thanks in advance!
> Raoul
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Select-a-set-of-rows-based-on-a-condition-tp4168594p4168594.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Select a set of rows based on a condition

RaoulD
Thanks a million Michael! Works perfectly! Really appreciate it!
Loading...