Ching-Chuan Chen's Blogger

Statistics, Machine Learning and Programming

0%

Transform Variables of R data.frame

有人在PTT問說,怎樣轉換R data.frame的多行column,連結:PTT文章

提醒:下面程式沒有判斷factor,如有factor請自行加入判斷式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# data.table做法:
library(data.table)
DT[ , lapply(.SD, function(x)iconv(x,"UTF8", "BIG5"))]

# 如果有numeric或是integer column的話:
DT[ , lapply(.SD, function(x){
if (is.character(x))
iconv(x,"UTF8", "BIG5")
} else return(x)})]


# dplyr做法:
library(dplyr)
DF %>% mutate_each(funs(iconv(., "UTF8", "BIG5")))

# 如果有numeric或是integer column的話:
DF %>% mutate_if(is.character, funs(iconv(., "UTF8", "BIG5")))

# base函數解法:
evalExpr <- lapply(names(DF), function(x){
bquote(iconv(.(as.symbol(x)), "UTF8", "BIG5"))
})
do.call(function(...) transform(DF, ...), evalExpr)

# 如果有numeric或是integer column的話:
evalExpr <- lapply(names(DF)[sapply(DF, is.character)],
function(x) bquote(iconv(.(as.symbol(x)), "UTF8", "BIG5")))
do.call(function(...) transform(DF, ...), evalExpr)

當然也可以直接用column name帶入去做轉換:

1
2
3
4
5
6
7
8
9
lapply(names(DF), function(x){
iconv(DF[[x]], "UTF8", "BIG5")
})

# 或是

lapply(names(DF)[sapply(DF, is.character)], function(x){
iconv(DF[[x]], "UTF8", "BIG5")
})

只是除了要多轉一次data.frame之外,第二個還要把numeric, integer column併回去