这期内容当中小编将会给大家带来有关R语言中RStudio中文读取乱码的解决方法,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
RStudio中使用rjson包读取处理json数据时,遇到中文会显示为 “<U+4EBA>“等类似乱码。
解决方案,在R语言终端中输入以下命令完美解决。
Sys.setlocale(category="LC_ALL",locale="en_US.UTF-8")
将以下JSON数据保存为json格式文件(注意文件编码为UTF-8)
{ "ID": ["1", "2", "3", "4", "5", "6", "7", "8"], "Name": ["Rick", "Dan", "Michelle", "Ryan", "Gary", "Nina", "Simon", "Guru"], "Salary": ["623.3", "515.2", "611", "729", "843.25", "578", "632.8", "722.5"], "StartDate": ["1/1/2012", "9/23/2013", "11/15/2014", "5/11/2014", "3/27/2015", "5/21/2013", "7/30/2013", "6/17/2014"], "Dept": ["IT部门", "Operations", "IT", "HR人力资源部", "Finance", "IT", "Operations", "Finance"] }
使用rjson包读取json数据,已下3行命令:
library("rjson") result_json <- fromJSON(file = "comment.json") result_json
第一行:加载rjson包;
第二行:使用函数fromJSON读取本地json文件;
第三行:输出结果;
在未解决乱码之前输出结果是:
$ID [1] "1" "2" "3" "4" "5" "6" "7" "8" $Name [1] "Rick" "Dan" "Michelle" "Ryan" [5] "Gary" "Nina" "Simon" "Guru" $Salary [1] "623.3" "515.2" "611" "729" "843.25" "578" [7] "632.8" "722.5" $StartDate [1] "1/1/2012" "9/23/2013" "11/15/2014" "5/11/2014" [5] "3/27/2015" "5/21/2013" "7/30/2013" "6/17/2014" $Dept [1] "IT<U+90E8><U+95E8>" [2] "Operations" [3] "IT" [4] "HR<U+4EBA><U+529B><U+8D44><U+6E90><U+90E8>" [5] "Finance" [6] "IT" [7] "Operations" [8] "Finance"
通过命令解决中文输出问题后,输出结果完美解决中文问题。
$ID [1] "1" "2" "3" "4" "5" "6" "7" "8" $Name [1] "Rick" "Dan" "Michelle" "Ryan" [5] "Gary" "Nina" "Simon" "Guru" $Salary [1] "623.3" "515.2" "611" "729" "843.25" "578" [7] "632.8" "722.5" $StartDate [1] "1/1/2012" "9/23/2013" "11/15/2014" "5/11/2014" [5] "3/27/2015" "5/21/2013" "7/30/2013" "6/17/2014" $Dept [1] "IT部门" "Operations" "IT" [4] "HR人力资源部" "Finance" "IT" [7] "Operations" "Finance"
上述就是小编为大家分享的R语言中RStudio中文读取乱码的解决方法了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。