stringr-----str_conv
主页:https://cran.r-project.org/web/packages/stringr/index.html
#安装stringr包> install.packages('stringr')> library(stringr)
#stringr函数分类:
字符串拼接函数
字符串计算函数
字符串匹配函数
字符串变换函数
参数控制函数
#stringr字符串变换函数
str_conv(string, encoding)
string: 字符串,字符串向量。 encoding: 编码名。
对中文进行转码处理。
# 把中文字符字节化 > x <- charToRaw('你好');x [1] c4 e3 ba c3 # 默认win系统字符集为GBK,GB2312为GBK字集,转码正常 > str_conv(x, "GBK") [1] "你好" > str_conv(x, "GB2312") [1] "你好" # 转UTF-8失败 > str_conv(x, "UTF-8") [1] "���" Warning messages: 1: In stri_conv(string, encoding, "UTF-8") : input data \xffffffc4 in current source encoding could not be converted to Unicode 2: In stri_conv(string, encoding, "UTF-8") : input data \xffffffe3\xffffffba in current source encoding could not be converted to Unicode 3: In stri_conv(string, encoding, "UTF-8") : input data \xffffffc3 in current source encoding could not be converted to Unicode 把unicode转UTF-8 > x1 <- "\u5317\u4eac" > str_conv(x1, "UTF-8") [1] "北京"