如何使用多个条件和匹配来创建新变量?
How can I use multiple conditionals and match to create a new variable?
我有以下数据
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)
我想创建一个新列来标识特定球员的背靠背比赛(连续两天打一场比赛)。
列的输出:1(如果是 b2b)如果不是,则为 0。
b2b 的第一天和第二天都应该有一个 1。
这是一个拆分-应用-组合问题(因为您需要分别处理每个玩家),您可以在基础 R 中执行此操作(by()
、aggregate()
、...)或有各种包(plyr
、dplyr
、data.table
)...这是一个 plyr()
解决方案。
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)))
我的代码已按字母顺序自动重新组织玩家(因为玩家变成了一个按字母顺序排列的因素,ddply
returns 数据按此重新排列的顺序排列)。如果这很重要,您可以确保在开始之前按照您想要的方式对因素进行排序。
我有以下数据
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)
我想创建一个新列来标识特定球员的背靠背比赛(连续两天打一场比赛)。
列的输出:1(如果是 b2b)如果不是,则为 0。
b2b 的第一天和第二天都应该有一个 1。
这是一个拆分-应用-组合问题(因为您需要分别处理每个玩家),您可以在基础 R 中执行此操作(by()
、aggregate()
、...)或有各种包(plyr
、dplyr
、data.table
)...这是一个 plyr()
解决方案。
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)))
我的代码已按字母顺序自动重新组织玩家(因为玩家变成了一个按字母顺序排列的因素,ddply
returns 数据按此重新排列的顺序排列)。如果这很重要,您可以确保在开始之前按照您想要的方式对因素进行排序。