R-为什么X添加到我的数据框架中的变量的名称? [英] R- Why are Xs added to the names of variables in my data frame?

查看:328
本文介绍了R-为什么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)

这篇关于R-为什么X添加到我的数据框架中的变量的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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