R语言 读取csv文件 解决分割符不能正确识别导致的错位现象 您所在的位置:网站首页 r语言导入数据无法打开链接 R语言 读取csv文件 解决分割符不能正确识别导致的错位现象

R语言 读取csv文件 解决分割符不能正确识别导致的错位现象

2023-06-29 14:40| 来源: 网络整理| 查看: 265

R语言 读取csv文件 解决分割符不能正确识别导致的错位现象

看到不少童鞋都遇到过类似问题。 使用python爬取了一些微博数据,存储在csv文件中: 已经爬取数据(存储在csv文件) 需要导入R中进行数据清洗和绘图,使用read.csv()函数读取(下面是有问题的,最常用读取csv文件的代码):

path = "D:/资料/eclipse/work-pace/weibo-crawler-master/weibo" setwd(path) #获得csv文件列表 df = read.csv(file_list[1],header=F,sep = ',',comment.char = '', na.strings = '',fill = T,quote = '\"',encoding = 'UTF-8') # file_list是文件全目录,file_list[1]是其中一个文件的全目录

读取后在Rstudio查看: 在这里插入图片描述 在这里插入图片描述 前几列是正常的,但是从第10列开始出问题了,有一些分隔符被识别为了文本,出现错列。导致最后一列情感得分(grade)也出现问题(图片未展示最后一列)

解决方案一 把csv、txt文件用别的工具(如notepad++)转成 ansi 编码的,然后再用read.csv命令去读。 缺陷:要手动去做 解决方案二 有博主指出是由于文件中文本双引号导致的问题,替换文件中的双引号为单引号即可。我没有试过,不知是否可行。 解决方案三 最简单的办法。直接使用fread()函数

library(data.table) df = fread(file = file_list[1],encoding = 'UTF-8') #file_list[1]是文件全目录

读取后最后几列效果: 在这里插入图片描述 最后几列非常完美,列名也自动读出。 附上函数的介绍:

Fast and friendly file finagler Description: Similar to read.table but faster and more convenient. All controls such as sep, colClasses and nrows are automatically detected. bit64::integer64 types are also detected and read directly without needing to read as character before converting.

Dates are read as character currently. They can be converted afterwards using the excellent fasttime package or standard base functions.

‘fread’ is for regular delimited files; i.e., where every row has the same number of columns. In future, secondary separator (sep2) may be specified within each column. Such columns will be read as type list where each cell is itself a vector.



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有