[R] accessing members from a data frame

Phil Spector spector at stat.berkeley.edu
Thu Feb 10 21:54:28 CET 2011


Try
   my.data.frame[my.data.frame$Buy==1 | my.data.frame$Sell ==1, ]
or
   subset(my.data.frame,buy == 1 | sell == 1)

Then take a look at the help page for ||  i.e., help("||")
to see what you did wrong.

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu



On Thu, 10 Feb 2011, Kushan Thakkar wrote:

> I have a data frame with the following columns:
>
> Date Price Buy Sell
>
> The Buy and Sell variables are binary. They are either zero or 1.
> Additionally, they are mutually exclusive. Either Buy is 1, Sell is 1 or
> they are both 0. But they are never both 1 for a given observation.
>
> I want to see observations for either either Buy == 1 or Sell ==1 (but not
> the ones where Buy == 0 and Sell ==0). How can I do this WITHOUT explicitly
> looping using a for loop.
>
> The options I have tried so far are:
>
> my.data.frame[my.data.frame$Buy==1 || my.data.frame$Sell ==1, ]
>
> This just prints the column headers with no observation:
>
> [1] Date  Price Buy   Sell
> <0 rows> (or 0-length row.names)
>
> I also tried passing argument as a vector
>
> my.data.frame[c(my.data.frame$Buy==1, my.data.frame$Sell ==1),]
>
> Interestingly enough, this returns all the rows where $Buy ==1. And then it
> returns a bunch of rows with NAs:
>
> NA          <NA>      NA      NA      NA
> NA.1        <NA>      NA      NA      NA
> NA.2        <NA>      NA      NA      NA
> NA.3        <NA>      NA      NA      NA
> NA.4        <NA>      NA      NA      NA
> NA.5        <NA>      NA      NA      NA
> NA.6        <NA>      NA      NA      NA
> NA.7        <NA>      NA      NA      NA
> NA.8        <NA>      NA      NA      NA
>
> Any help on how I can make this happen?
>
> 	[[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.
>



More information about the R-help mailing list