R中的fread data.table不读入列名 [英] fread data.table in R doesn't read in column names

查看:516
本文介绍了R中的fread data.table不读入列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将数据文件读入R时,我可以将其读为 data.frame data.table 使用 data.table 包。我希望以后使用 data.table ,因为它可以更好地处理大数据。但是,这两种方法都存在问题(data.frames为 read.table ,data.tables为 fread )和我想知道是否有一个简单的解决方法。

When reading in a data file into R, I can read it in either as a data.frame or a data.table using the data.table package. I would prefer to use data.table in the future since it deals with large data better. However, there are issues with both methods (read.table for data.frames, fread for data.tables) and I'm wondering if there's a simple fix out there.

当我使用 read.table 生成 data.frame ,如果我的列名包含冒号或空格,则将它们替换为句号,这是我所不希望的。我希望按原样读取列名称。

When I use read.table to produce a data.frame, if my column names include colons or spaces, they are replaced by periods, which I do not want. I want the column names to be read in "as is."

或者,当我使用 fread 生成时一个 data.table ,我的列名根本没有读入,这显然是不希望的。

Alternatively, when I use fread to produce a data.table, my column names are not read in at all, which is obviously not desired.

检查在下面的要点中找到一个可重现的示例:

Check out this gist below for a reproducible example:

https ://gist.github.com/jeffbruce/b966d41eedc2662bbd4a

欢呼声

推荐答案

R总是尝试转换列名以确保它们是有效的变量名,因此它添加了点号来代替空格和冒号。如果您不希望在使用 read.table

R always try to convert column names to ensure that they are valid variable names, hence it adds periods in place of spaces and colons. If you dont want that you can use check.names=FALSE while using read.table

df1<-read.table("data.txt",check.names = FALSE)

sample(colnames(df1),10)
 [1] "simple lobule white matter"                       
 [2] "anterior lobule white matter"                     
 [3] "hippocampus"                                      
 [4] "lateral olfactory tract"                          
 [5] "lobules 1-2: lingula and central lobule (ventral)"
 [6] "Medial parietal association cortex"               
 [7] "Primary somatosensory cortex: trunk region"       
 [8] "midbrain"                                         
 [9] "Secondary auditory cortex: ventral area"          
[10] "Primary somatosensory cortex: forelimb region"  

您会看到姓氏保持不变。

这篇关于R中的fread data.table不读入列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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