R can't specify file encoding when loading from file. So we have to convert encoding after loading. The following codes convert data with GBK encoding (which can't display properly in browser connected to a rstudio-server on Ubuntu), to UTF-8 encoding, which can display properly:
> load("gbkobj.RData")
> head(gbkobj)
\xc9\xe7\xc7\xf8 \xd0Ա\xf0 ̬\xb6\xc8
1 A\xc9\xe7\xc7\xf8 \xc4\xd0 \xb7\xb4\xb6\xd4
2 B\xc9\xe7\xc7\xf8 Ů \xb7\xb4\xb6\xd4
3 D\xc9\xe7\xc7\xf8 Ů \xb7\xb4\xb6\xd4
4 C\xc9\xe7\xc7\xf8 \xc4\xd0 \xb7\xb4\xb6\xd4
5 A\xc9\xe7\xc7\xf8 \xc4\xd0 \xd4\u07b3\xc9
6 D\xc9\xe7\xc7\xf8 Ů \xb7\xb4\xb6\xd4
> colnames(gbkobj) <- iconv(colnames(gbkobj), from = "GBK", to = "UTF-8")
> res <- as.data.frame(lapply(gbkobj, function(x) iconv(x, from = "GBK", to = "UTF-8")))
> head(res)
社区 性别 态度
1 A社区 男 反对
2 B社区 女 反对
3 D社区 女 反对
4 C社区 男 反对
5 A社区 男 赞成
6 D社区 女 反对
Note: apply
will convert a data frame to a matrix before applying functions.
So here we use lapply
instead.
But lapply
return a list, so we convert it to a data frame with
as.data.frame
function.