如何只:
# create df1 with 10 random TRUE/FALSE values
df1 <- data.frame(Player= sample(c(TRUE,FALSE),10,TRUE))
df1$playing <- sapply(df1$Player, function(x) ifelse(x==TRUE,1,0))
更新:
require(tidyr)
df1 <- data.frame(ID = seq(1,35),Player= sample(c("TRUE","FALSE"),35,TRUE))
df1$playing <- sapply(df1$Player, function(x) ifelse(x==TRUE,1,0))
df1 <- df1[,-2]
spread(df1, ID, playing)
並推廣到多個遊戲(即每個玩家ID多行)
df1 <- data.frame(ID = rep(1:35,each=3),GameID=rep(1:3,35),Player= sample(c("TRUE","FALSE"),105,TRUE))
df1$playing <- sapply(df1$Player, function(x) ifelse(x==TRUE,1,0))
df1 <- df1[,-3]
spread(df1, ID, playing)
樣本輸出
GameID 1 2 3 4
1 0 1 0 0
2 0 0 1 0
3 0 1 0 0
請仔細閱讀[如何使R中一個偉大的可重複的例子?(https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Masoud