[R] Accessing Data Frame

Jeff Newmiller jdnewmil @ending from dcn@d@vi@@c@@u@
Thu Jan 3 18:33:00 CET 2019


You would be better served to reference SQL semantics (relational identity) than Network database semantics (object identifiers) for understanding data frames. The row in `aCard` is not the same as the row in `deck`, and you should not construct your algorithms based on individual rows but rather as sets of rows.

On January 3, 2019 9:15:41 AM PST, Benoit Galarneau <benoit.galarneau using polymtl.ca> wrote:
>You are correct, the anti_join is working fine.
>However, I still find it strange there is no "quick" way to find the  
>index of an item extracted from the data frame.
>
>This works as it returns the deck without the card no 10.
>aCard = deck[10,]
>cardNo = which(deck$value == aCard$value & deck$suit == aCard$suit)
>deck[-cardNo,]
>
>But I'm still puzzled by the complexity of finding back the index of  
>the card with the long statement.
>
>Another approach that "works" is the following, but I still find it  
>strange to depend on data frame row names to find the index:
>cardNo <- as.numeric(row.names(aCard))
>
>Apologies if the above question are strange. I'm coming C++ world with 
>
>some bias with objects. Again, since "aCard" is extracted from the  
>data frame, I assume (bias?) there would be a simple way to find back  
>the item in the data frame it came frame. Some kind of indexOf() or  
>similar on the container and item.
>
>Benoit
>
>Ista Zahn <istazahn using gmail.com> a écrit :
>
>> Hi Benoit,
>>
>> You can select rows from deck matched in aCard using
>>
>> merge(deck, aCard)
>>
>> Selecting rows that don't match is bit more difficult. You could do
>> something like
>>
>> isin <- apply(mapply(function(x, y) x %in% y, deck, topCard),
>>                1,
>>                all)
>> deck[!isin, ]
>>
>> perhaps.
>>
>> Alternatively, you can use anti_join from the dplyr package:
>>
>> library(dplyr)
>> anti_join(deck, topCard)
>>
>> Best,
>> Ista
>>
>> On Thu, Jan 3, 2019 at 10:38 AM Benoit Galarneau
>> <benoit.galarneau using polymtl.ca> 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
>>>
>>> ______________________________________________
>>> R-help using r-project.org 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.
>
>______________________________________________
>R-help using r-project.org 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.

-- 
Sent from my phone. Please excuse my brevity.



More information about the R-help mailing list