为什么在使用read.csv时将X添加到数据帧变量名称中? [英] Why are Xs added to data frame variable names when using read.csv?

查看:181
本文介绍了为什么在使用read.csv时将X添加到数据帧变量名称中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 R 中使用 read.csv()函数加载数据时,我经常发现X已添加到变量名。我想我几乎总是在第一个变量中看到它,但是我可能是错的。

When I use the read.csv() function in R to load data, I often find that an X has been added to variable names. I think I just about always see it it in the first variable, but I could be wrong.

起初,我以为 R 可能会这样做,因为在变量名的开头有一个空格- 我不。

At first, I thought R might be doing this because I had a space at the beginning of the variable name - I don't.

第二,我在某处读到,如果您有一个以数字开头或很短的变量名 R 将添加X。变量名称为全文本,并且此变量名称的长度为12个字符,因此长度不短。

Second, I had read somewhere that if you have a variable that starts with a number, or is a very short variable name, R would add the X. The variable name is all text and the length of the name of this variable is 12 characters, so it's not short.

现在,这真是个烦人。我可以重命名该列,但它确实增加了一个步骤,尽管很小。

Now, this is purely an annoyance. I can rename the column, but it does add a step, albeit a small one.

有没有办法防止流氓X渗透到我的数据框中?

Is there a way to prevent this from rogue X from infiltrating my data frame?

这是我的原始代码:

df <- read.csv("/file/location.filecsv", header=T, sep=",")

这里是有问题的变量:

str(orders)
'data.frame':   2620276 obs. of  26 variables:
 $ X.OrderDetailID    : Factor w/ 2620193 levels "(2620182 row(s) affected)",..: 105845


推荐答案

read.table read.csv 有一个 check.names = 参数,您可以将其设置为 FALSE

read.table and read.csv have a check.names= argument that you can set to FALSE.

例如,尝试使用仅包含标题的输入:

For example, try it with this input consisting of just a header:

> read.csv(text = "a,1,b")
[1] a  X1 b 
<0 rows> (or 0-length row.names)

> read.csv(text = "a,1,b", check.names = FALSE)
[1] a 1 b
<0 rows> (or 0-length row.names)

这篇关于为什么在使用read.csv时将X添加到数据帧变量名称中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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