fread-将所有列读取为字符 [英] fread - read all columns as character

查看:155
本文介绍了fread-将所有列读取为字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 data.table / fread 将文件读入R。有些字段的前导零,我只想读取字符中的数据并手动修复它们。但是我不知道如何将其传达给 fread 。我正在尝试此操作,并且它通常分配char,num等类型:

I'm trying to read a file into R using data.table / fread. Some of the fields have leading zeros and I just want to read the data in as characters and manually fix them. However I can't figure out how to convey this to fread. I'm trying this and it's assigning char, num, etc types as it normally would :

prop1 <- data.frame(fread("C:\\myFile.csv"), stringsAsFactors = F, colClasses = c(rep('character',58)))

我想念什么?

推荐答案

您的 colClasses 参数的位置错误。它必须在 fread()内部,而不是在 data.frame()内部。尝试以下操作:

Your colClasses argument is in the wrong place. It needs to be inside fread(), not inside data.frame(). Try this:

prop1 <- data.frame(fread("C:\\myFile.csv", 
                          colClasses = c(rep("character", 58))),
                    stringsAsFactors = FALSE)

更规范地使用 data.table 可以做到这一点:

More canonical use of data.table to accomplish this would be:

prop1 <- fread("C:\\myfile.csv", colClasses = 'character', data.table = FALSE)

这篇关于fread-将所有列读取为字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆