如何根据R中另一列中的特定文本创建二进制列?

时间:2018-06-11 04:23:15

标签: r

我在R工作,我需要创建一个二进制列,根据玩家是否有名称Mike(0表示否,1表示是)。这是我到目前为止的代码,但我不知道下一步该做什么:

baseball$player_mike <- ifelse(baseball$player_name == "%Mike%", 1, 0)

baseball是数据框,player_mike是二进制列的内容,player_name是播放器列表(全名)。任何关于我做错的指导都将不胜感激,谢谢!

2 个答案:

答案 0 :(得分:0)

自从您第一次使用tidyverse以来,就只是一种附加的ifelse处理方法。

library(dplyr)
output <- baseball %>%
  dplyr::mutate(
    player_mike = ifelse(player_name == "Mike", 1, 0)
  )

如果您同时有mikeMike的实例,则可以进行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