Creating a new variable from a lookup table

后端 未结 4 800
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 04:39

I have the following columns in my data set:

presult     aresult
  I         single
  I         double
  I         triple
  I         home run
  SS        st         


        
4条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 05:18

    An alternative to Dieter's answer:

    dat <- data.frame(
      presult = c(rep("I", 4), "SS", "ZZ"),
      aresult = c("single", "double", "triple", "home run", "strikeout", "home run"),
      stringsAsFactors=FALSE
    )
    
    dat$base <- as.integer(factor(dat$aresult,
      levels=c("strikeout","single","double","triple","home run")))-1
    

提交回复
热议问题