celestialgod

transform variables

Apr 14th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.87 KB | None | 0 0
  1. # data.table做法:
  2. library(data.table)
  3. DT[ , lapply(.SD, function(x)iconv(x,"UTF8", "BIG5"))]
  4.  
  5. # 如果有numeric或是integer column的話:
  6. DT[ , lapply(.SD, function(x){
  7.   if (is.character(x))
  8.     iconv(x,"UTF8", "BIG5")
  9. } else return(x)})]
  10.  
  11.  
  12. # dplyr做法:
  13. library(dplyr)
  14. DF %>% mutate_each(funs(iconv(., "UTF8", "BIG5")))
  15.  
  16. # 如果有numeric或是integer column的話:
  17. DF %>% mutate_if(is.character, funs(iconv(., "UTF8", "BIG5")))    
  18.  
  19. # base函數解法:
  20. evalExpr <- lapply(names(DF), function(x){
  21.   bquote(iconv(.(as.symbol(x)), "UTF8",  "BIG5"))
  22. })
  23. do.call(function(...) transform(DF, ...), evalExpr)
  24.  
  25. # 如果有numeric或是integer column的話:
  26. evalExpr <- lapply(names(DF)[sapply(DF, is.character)],
  27.                    function(x) bquote(iconv(.(as.symbol(x)), "UTF8",  "BIG5")))
  28. do.call(function(...) transform(DF, ...), evalExpr)
Advertisement
Add Comment
Please, Sign In to add comment