You can extract the face column with R’s $ notation: R won’t return a TRUE or FALSE, because it won’t have to: a will equal b, because you just ran the equivalent of a <- b. Unfortunately, you’ll be in for a nasty surprise. It is easy to forget and use a = b to test if a equals b. Notice that you test for equality with a double equals sign, =, and not a single equals sign, =, which is another way to write <. Instead, %in% will independently test whether each value on the left is somewhere in the vector on the right: 1 %in% c( 3, 4, 5) If you provide a vector on the left side, %in% will not pair up the values on the left with the values on the right and then do element-wise tests. %in% tests whether the value(s) on the left side are in the vector on the right side. %in% is the only operator that does not do normal element-wise execution. Table 7.1: R’s Logical Operators Operator R provides seven logical operators that you can use to make comparisons, shown in Table 7.1. Logical subsetting provides a way to do targeted extraction and modification with R objects, a sort of search-and-destroy mission inside your own data sets.Ī logical test is a comparison like “is one less than two?”, 1 4. Why not ask R to find the aces for you? You can do this with logical subsetting. If your data frame were larger, it might be impossible: deck3 <- shuffle(deck) The cards were sorted in an orderly manner and an ace appeared every 13 rows.īut what if the deck had been shuffled? You could look through all the cards and note the locations of the aces, but that would be tedious. Things worked very easily in this example because you knew exactly where each ace was. Just describe the values that you want to change with R’s notation system, then assign over those values with R’s assignment operator. The same technique will work whether you store your data in a vector, matrix, array, list, or data frame. You don’t end up with a modified copy of deck2 the new values will appear inside deck2: head(deck2, 13) So you could save c(14, 14, 14, 14) into the ace values, or you could just save 14 and rely on R’s recycling rules to expand 14 to c(14, 14, 14, 14): deck2 $value <- c( 14, 14, 14, 14) The set of new values will have to be the same size as the set of values that you are replacing. Now all you have to do is assign a new set of values to these old values. Or, even better, you can subset the column vector deck2$value: deck2 You can single out just the values of the aces by subsetting the columns dimension of deck2. Hence, you can describe them with R’s notation system: deck2 To play war, you just need to change the values of your aces from 1 to 14.Īs long as you haven’t shuffled your deck, you know just where the aces are. Every other card gets the value that it already has in deck. They receive the highest value of all the cards, which would be something like 14. In the game of war, aces are king (figuratively speaking). You can also remove columns from a data frame (and elements from a list) by assigning them the symbol NULL: deck2 $new <- NULL head(deck2) This provides a great way to add new variables to your data set: deck2 $new <- 1 : 52 head(deck2) R will expand the object to accommodate the new values: vec <- 0 You can also create values that do not yet exist in your object. You can replace multiple values at once as long as the number of new values equals the number of selected values: vec <- c( 1, 1, 1) Here’s how you can select the first value of vec: vecĪnd here is how you can modify it: vec <- 1000 Let’s put this into action with a real example: vec <- c( 0, 0, 0, 0, 0, 0) R will update the selected values in the original object. Then use the assignment operator <- to overwrite those values. First, describe the value (or values) you wish to modify. You can use R’s notation system to modify values within an R object.
0 Comments
Leave a Reply. |