Just switch that last line in the switch function to use x instead of test$IDswfun <- function(x) { switch(x, 'joe.schmoe' = 'JS', 'teddy.roos' = 'TR', 'sally.fields' = 'SF', as.character(x) ) }结果> unname(sapply(ID, swfun)) [1] "SPW" "SM" "DLS" "SJ" "JS" "CEJ" "TR" "GVF" "MJC"[10] "LH" "SF"Edit re Vectorize (Frank):如果你发现自己sapply经常这样做,你可能会考虑Edit re Vectorize (Frank): If you find yourself sapplying this often, you might considerswfun2 <- Vectorize(swfun)unname(swfun2(ID)) [1] "SPW" "SM" "DLS" "SJ" "JS" "CEJ" "TR" "GVF" "MJC"[10] "LH" "SF"或下面评论中链接的替代方案.or the alternative linked in the comments below. 这篇关于列值的切换功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-04 20:52