Skip to content
Prev 377990 / 398502 Next

Accessing Data Frame

On 03/01/2019 12:39 p.m., Benoit Galarneau wrote:
I think the answer to "recommended or good practice" depends on your 
priorities.  If I was running a big simulation where speed really 
matters, I wouldn't do it that way:  dataframes are slow and looking for 
a name in a vector of names can be slow.  You'd get faster results using 
matrices and row indices (which can be negated to remove items, as you 
know.)  But then you have to deal with the issue that matrices can't mix 
types as your dataframe does, so your code is likely to be less clear.

If speed isn't so much of an issue but clarity is, then you really want 
to write your own small functions to do the removal, e.g.

whichCard <- function(deck, card) {
   which(deck$face == card$face & deck$suit == card$suit & deck$value = 
card$value)
}

removeCard <- function(deck, card) {
   deck[-whichCard(deck, card), ]
}

deck <- removeCard(deck, topCard)

Duncan Murdoch