See below.
> On Jan 3, 2019, at 6:50 AM, Benoit Galarneau <[email protected]>
> wrote:
>
> Hi everyone,
> I'm new to the R world.
> Probably a newbie question but I am stuck with some concept with data frame.
> I am following some examples in the "Hands-On Programming with R".
>
> In short, how can I access/filter items in a data frame using a variable.
>
> One example consists of manipulating elements from a deck of card:
>
>> deck
> face suit value
> 1 king spades 13
> 2 queen spades 12
> 3 jack spades 11
> 4 ten spades 10
> etc.
>
> Let's say I want to remove or filter out the first card. I know I could do
> deck[-1].
>
> But let's say I have: topCard <- deck[1,]
>
> topCard is then a list of 3 elements
>> topCard
> face suit value
> 1 king spades 13
>
> My question is the following, how can I remove or filter out the deck using
> the topCard variable.
>
> In my programmer's head, something similar to this should "work":
>> deck[10,]
> face suit value
> 10 four spades 4
>> aCard <- deck[10,]
>> aCard
> face suit value
> 10 four spades 4
>> deck[aCard]
> Error in `[.default`(deck, aCard) : invalid subscript type 'list'
>
> Wihout having to specify all elements in the logical tests.
>
> deck[deck$face == aCard$face & deck$suit == aCard$suit & deck$value ==
> aCard$value,]
> face suit value
> 10 four spades 4
matchRow <- Reduce( "&", mapply("==", deck, aCard, SIMPLIFY=FALSE))
deck[ !matchRow, ]
There is a little `trick' here. A "data.frame" object is a "list" object and
both `deck` and `aCard` are "data.frame" objects. So using `mapply` as above
operates on the successive columns of each.
You will find many useful idioms using the *apply functions.
As you are new to R, I recommend running the example() for each of them as well
as browsing the help page.
HTH,
Chuck
______________________________________________
[email protected] mailing list -- To UNSUBSCRIBE and more, see
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.