s-news
[Top] [All Lists]

Re: Selecting every other line from a data frame

To: "Khan, Sohail" <khan@cshl.edu>
Subject: Re: Selecting every other line from a data frame
From: Tim Hesterberg <timh@insightful.com>
Date: 21 Apr 2006 10:49:57 -0700
Cc: <s-news@lists.biostat.wustl.edu>
In-reply-to: <C8696843AE995F4EA4CDC3E2B83482A901879010@mailbox02.cshl.edu> (khan@cshl.edu)
References: <C8696843AE995F4EA4CDC3E2B83482A901879010@mailbox02.cshl.edu>
>I have a large data frame, from which I want to only select every
>other line or odd lines.
>How can I do this?  Thanks.

Some other responses have suggested basically:
        x[ vector of logical values with alternating T/F, ]

I have two comments:

(1) It is safer to add drop=FALSE
        x[ vector of logical values with alternating T/F, , drop=FALSE]
in case the data frame has only one column.

(2) You could instead do e.g.:
        x[ seq(from = 1, to = nrow(x), by=2), , drop = FALSE]
However, you should only do this if the data frame has a dup.row.names
attribute, otherwise this would incur a large performance hit
in checking for duplicated row names in the output.

Tim Hesterberg


<Prev in Thread] Current Thread [Next in Thread>