How can I use multiple conditionals and match to create a new variable?

Sburg13

I have the following data

Name <- c("Kobe Bryant", "Kobe Bryant", "Kobe Bryant", 
          "Kobe Bryant", "Kobe Bryant", "Kobe Bryant", 
          "Lebron James", "Lebron James", "Lebron James", 
          "Lebron James", "Kevin Durant", "Kevin Durant",
          "Kevin Durant", "Kevin Durant", "Kevin Durant")

Date <- as.Date(c("2015-05-14", "2015-05-15", "2015-05-19", "2015-05-21", 
           "2015-05-24", "2015-05-28", "2015-05-14", "2015-05-20", 
           "2015-05-21", "2015-05-23", "2015-05-22", "2015-05-24", 
           "2015-05-28", "2015-06-02", ""2015-06-04"))

df <- data.frame c(Name, Date)

Desired_output <- c(1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0)

df2 <- data.frame c(Name, Date, Desired_output)

I want to create a new column that identifies the back-to-back games (playing a game two consecutive days) for a specific player.

Output of the column: 1 (if b2b) 0 if not.

Both the first day and the second day of the b2b should have a 1.

Ben Bolker

This is a split-apply-combine problem (since you need to handle each player separately), which you can do in base R (by(), aggregate(), ...) or with a variety of packages (plyr, dplyr, data.table) ... here's a plyr() solution.

Name <- rep(c("Kobe Bryant", "Lebron James", "Kevin Durant"),
            c(6,4,5))
Date <- as.Date(c("2015-05-14", "2015-05-15", "2015-05-19",
  "2015-05-21","2015-05-12", "2015-05-28", "2015-05-14",
  "2015-05-16","2015-05-17", "2015-05-21", "2015-05-22",
  "2015-05-24","2015-05-28","2015-06-02","2015-06-10"))
dd <- data.frame(Name,Date)
b2b <- function(x,ind=FALSE) {
    x0 <- head(x,-1)  ## all but last
    x1 <- tail(x,-1)  ## all but first
    comp <- abs(head(x,-1)-tail(x,-1))==1
    res <- c(comp,FALSE) | c(FALSE,comp)
    if (ind) {
        w <- res==1 & c(0,res[-length(res)])==1
        res[w] <- 2
    }
    return(res)
}
library("plyr")
ddply(dd,"Name",
      transform,
         b2b=as.numeric(b2b(Date)),
         b2b_ind=as.numeric(b2b(Date,ind=TRUE)))

My code has automatically reorganized the players by alphabetical order (because players got turned into a factor with levels in alphabetical order, and ddply returns the data in this rearranged order). If that's important you can make sure the factors are ordered the way you want before beginning.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I create a new instance of ImmutableDictionary?

From Dev

How can I create a JSON object that can handle conditionals?

From Dev

How can I create a new Symfony project with the new directory structure?

From Dev

PHP How can I create multiple sessions?

From Dev

How can I compute element-wise conditionals on batches in TensorFlow?

From Dev

How can I create a new socket in /dev?

From Dev

How can I create multiple SSH keys?

From Dev

How can I create a new table in my SQLite Database using a param from a Java String variable?

From Dev

How can I use a loop index to reference variable names and create new variable names?

From Dev

Python: Conditionals for new variable with three classes

From Dev

How can I create a new socket in /dev?

From Dev

How can I create multiple SSH keys?

From Dev

How do I use conditionals in Angular Expressions with ngDisable?

From Dev

How i can match multiple CSS selectors

From Dev

How can I create a Boxplot for one Variable?

From Dev

How can I create new shell commands?

From Dev

How can I use awk to create a variable that is used in the next line edit

From Dev

How can I create multiple users in Unix?

From Dev

How can I use preg_match to match integer or double

From Dev

Can I initialize an array with conditionals?

From Dev

How to use conditionals with lists

From Dev

How can I use Global variable across multiple functions in JavaScript?

From Dev

How can I use a bash variable as a regex argument to gsub() and match()?

From Dev

How can I edit columns of numbers in vim using conditionals?

From Dev

How can I create multiple functions with variable in name?

From Dev

Use lapply to create new variable over multiple data frames

From Dev

How can i join a string and a variable to create a new variable in javascript?

From Dev

How to create new vector by using conditionals on another vector?

From Dev

How can I use a formula to match multiple criteria from a different table in Excel?

Related Related

  1. 1

    How can I create a new instance of ImmutableDictionary?

  2. 2

    How can I create a JSON object that can handle conditionals?

  3. 3

    How can I create a new Symfony project with the new directory structure?

  4. 4

    PHP How can I create multiple sessions?

  5. 5

    How can I compute element-wise conditionals on batches in TensorFlow?

  6. 6

    How can I create a new socket in /dev?

  7. 7

    How can I create multiple SSH keys?

  8. 8

    How can I create a new table in my SQLite Database using a param from a Java String variable?

  9. 9

    How can I use a loop index to reference variable names and create new variable names?

  10. 10

    Python: Conditionals for new variable with three classes

  11. 11

    How can I create a new socket in /dev?

  12. 12

    How can I create multiple SSH keys?

  13. 13

    How do I use conditionals in Angular Expressions with ngDisable?

  14. 14

    How i can match multiple CSS selectors

  15. 15

    How can I create a Boxplot for one Variable?

  16. 16

    How can I create new shell commands?

  17. 17

    How can I use awk to create a variable that is used in the next line edit

  18. 18

    How can I create multiple users in Unix?

  19. 19

    How can I use preg_match to match integer or double

  20. 20

    Can I initialize an array with conditionals?

  21. 21

    How to use conditionals with lists

  22. 22

    How can I use Global variable across multiple functions in JavaScript?

  23. 23

    How can I use a bash variable as a regex argument to gsub() and match()?

  24. 24

    How can I edit columns of numbers in vim using conditionals?

  25. 25

    How can I create multiple functions with variable in name?

  26. 26

    Use lapply to create new variable over multiple data frames

  27. 27

    How can i join a string and a variable to create a new variable in javascript?

  28. 28

    How to create new vector by using conditionals on another vector?

  29. 29

    How can I use a formula to match multiple criteria from a different table in Excel?

HotTag

Archive