数据框列命名

发布于 2024-12-04 07:44:42 字数 190 浏览 0 评论 0原文

我正在创建一个像这样的简单数据框:

qcCtrl <- data.frame("2D6"="DNS00012345", "3A4"="DNS000013579")

我的理解是列名称应该是“2D6”和“3A4”,但它们实际上是“X2D6”和“X3A4”。为什么要添加 X 以及如何停止这种情况?

I am creating a simple data frame like this:

qcCtrl <- data.frame("2D6"="DNS00012345", "3A4"="DNS000013579")

My understanding is that the column names should be "2D6" and "3A4", but they are actually "X2D6" and "X3A4". Why are the X's being added and how do I make that stop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃兔兔 2024-12-11 07:44:42

我不建议使用以数字开头的列名称,但如果您坚持,请使用 data.framecheck.names=FALSE 参数:

qcCtrl <- data.frame("2D6"="DNS00012345", "3A4"="DNS000013579", 
            check.names=FALSE)
qcCtrl

          2D6          3A4
1 DNS00012345 DNS000013579

我谨慎的原因之一与此相反,$ 运算符的使用变得更加棘手。例如,以下内容会失败并出现错误:

> qcCtrl$2D6
Error: unexpected numeric constant in "qcCtrl$2"

要解决此问题,无论何时使用列名,都必须将其括在反引号中:

> qcCtrl

我不建议使用以数字开头的列名称,但如果您坚持,请使用 data.framecheck.names=FALSE 参数:

qcCtrl <- data.frame("2D6"="DNS00012345", "3A4"="DNS000013579", 
            check.names=FALSE)
qcCtrl

          2D6          3A4
1 DNS00012345 DNS000013579

我谨慎的原因之一与此相反,$ 运算符的使用变得更加棘手。例如,以下内容会失败并出现错误:

> qcCtrl$2D6
Error: unexpected numeric constant in "qcCtrl$2"

要解决此问题,无论何时使用列名,都必须将其括在反引号中:

2D6` [1] DNS00012345 Levels: DNS00012345

I do not recommend working with column names starting with numbers, but if you insist, use the check.names=FALSE argument of data.frame:

qcCtrl <- data.frame("2D6"="DNS00012345", "3A4"="DNS000013579", 
            check.names=FALSE)
qcCtrl

          2D6          3A4
1 DNS00012345 DNS000013579

One of the reasons I caution against this, is that the $ operator becomes more tricky to work with. For example, the following fails with an error:

> qcCtrl$2D6
Error: unexpected numeric constant in "qcCtrl$2"

To get round this, you have to enclose your column name in back-ticks whenever you work with it:

> qcCtrl

I do not recommend working with column names starting with numbers, but if you insist, use the check.names=FALSE argument of data.frame:

qcCtrl <- data.frame("2D6"="DNS00012345", "3A4"="DNS000013579", 
            check.names=FALSE)
qcCtrl

          2D6          3A4
1 DNS00012345 DNS000013579

One of the reasons I caution against this, is that the $ operator becomes more tricky to work with. For example, the following fails with an error:

> qcCtrl$2D6
Error: unexpected numeric constant in "qcCtrl$2"

To get round this, you have to enclose your column name in back-ticks whenever you work with it:

2D6` [1] DNS00012345 Levels: DNS00012345
吹梦到西洲 2024-12-11 07:44:42

添加 X 是因为 R 不喜欢将数字作为列名称的第一个字符。要关闭此功能,请使用 as.character() 告诉 R 数据框的列名称是字符向量。

The X is being added because R does not like having a number as the first character of a column name. To turn this off, use as.character() to tell R that the column name of your data frame is a character vector.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文