我在R工作,我需要创建一个二进制列,根据玩家是否有名称Mike(0表示否,1表示是)。这是我到目前为止的代码,但我不知道下一步该做什么:
baseball$player_mike <- ifelse(baseball$player_name == "%Mike%", 1, 0)
baseball
是数据框,player_mike
是二进制列的内容,player_name
是播放器列表(全名)。任何关于我做错的指导都将不胜感激,谢谢!
答案 0 :(得分:0)
自从您第一次使用tidyverse
以来,就只是一种附加的ifelse
处理方法。
library(dplyr)
output <- baseball %>%
dplyr::mutate(
player_mike = ifelse(player_name == "Mike", 1, 0)
)
如果您同时有mike
和Mike
的实例,则可以进行tolower(player_name)
来适应。
答案 1 :(得分:0)
data.table
的替代方案:
require(data.table) # loads library
setDT(baseball) # sets baseball as a data.table
baseball[, player_mike := 0L] # creates a column and assigns 0 to all its values
baseball[grepl("Mike", player_name, ignore.case = TRUE), player_mike := 1L]
# filters rows where "Mike" appears in the player_name and
# assigns 1 to player_mike of those rows