在 data.frame 中指定列名称会将空格更改为“.”

发布于 2024-09-13 02:58:08 字数 373 浏览 2 评论 0原文

假设我有一个 data.frame,如下所示:

x <- c(1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10)
df <- data.frame("Label 1"=x,"Label 2"=rnorm(100))

head(df,3)

returns:

  Label.1    Label.2
1       1  1.9825458
2       2 -0.4515584
3       3  0.6397516

如何让 R 停止自动用列名中的句点替换空格?即“Label 1”而不是“Label.1”。

Let's say I have a data.frame, like so:

x <- c(1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10)
df <- data.frame("Label 1"=x,"Label 2"=rnorm(100))

head(df,3)

returns:

  Label.1    Label.2
1       1  1.9825458
2       2 -0.4515584
3       3  0.6397516

How do I get R to stop automagically replacing the space with a period in the column name? ie, "Label 1" instead of "Label.1".

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

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

发布评论

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

评论(4

柒七 2024-09-20 02:58:08

您可以在 data.frame (以及 read.table)中设置 check.names = FALSE

df <- data.frame("Label 1" = 1:3, "Label 2" = rnorm(3), check.names = FALSE)

returns:

  Label 1    Label 2
1       1  0.2013347
2       2  1.8823111
3       3 -0.5233811

From ?数据.frame

check.names
合乎逻辑的。如果TRUE,则检查数据框中的变量名称以确保它们是语法上有效的变量名称并且不重复。如果有必要,它们会被调整(通过make.names),以便它们是这样的。


来自 ?make.names

语法上有效的名称由字母、数字和点或下划线字符组成,并以字母或点开头,后面不跟数字。 “.2way”等名称无效,保留字也无效。

所有无效字符都会转换为“.


此外,如果您需要使用 $ 对名称为“无效”的变量进行子集化,则可以使用反引号 `< /代码>。例如:

df

您可以在 data.frame (以及 read.table)中设置 check.names = FALSE

df <- data.frame("Label 1" = 1:3, "Label 2" = rnorm(3), check.names = FALSE)

returns:

  Label 1    Label 2
1       1  0.2013347
2       2  1.8823111
3       3 -0.5233811

From ?数据.frame

check.names
合乎逻辑的。如果TRUE,则检查数据框中的变量名称以确保它们是语法上有效的变量名称并且不重复。如果有必要,它们会被调整(通过make.names),以便它们是这样的。


来自 ?make.names

语法上有效的名称由字母、数字和点或下划线字符组成,并以字母或点开头,后面不跟数字。 “.2way”等名称无效,保留字也无效。

所有无效字符都会转换为“.


此外,如果您需要使用 $ 对名称为“无效”的变量进行子集化,则可以使用反引号 `< /代码>。例如:

Label 1`

You may set check.names = FALSE in data.frame (as well as in read.table):

df <- data.frame("Label 1" = 1:3, "Label 2" = rnorm(3), check.names = FALSE)

returns:

  Label 1    Label 2
1       1  0.2013347
2       2  1.8823111
3       3 -0.5233811

From ?data.frame:

check.names
logical. If TRUE then the names of the variables in the data frame are checked to ensure that they are syntactically valid variable names and are not duplicated. If necessary they are adjusted (by make.names) so that they are.


From ?make.names:

A syntactically valid name consists of letters, numbers and the dot or underline characters and starts with a letter or the dot not followed by a number. Names such as ".2way" are not valid, and neither are the reserved words.

All invalid characters are translated to "."


Also, if you need to subset a variable with an 'invalid' name using $, you can use backticks `. For example:

df

You may set check.names = FALSE in data.frame (as well as in read.table):

df <- data.frame("Label 1" = 1:3, "Label 2" = rnorm(3), check.names = FALSE)

returns:

  Label 1    Label 2
1       1  0.2013347
2       2  1.8823111
3       3 -0.5233811

From ?data.frame:

check.names
logical. If TRUE then the names of the variables in the data frame are checked to ensure that they are syntactically valid variable names and are not duplicated. If necessary they are adjusted (by make.names) so that they are.


From ?make.names:

A syntactically valid name consists of letters, numbers and the dot or underline characters and starts with a letter or the dot not followed by a number. Names such as ".2way" are not valid, and neither are the reserved words.

All invalid characters are translated to "."


Also, if you need to subset a variable with an 'invalid' name using $, you can use backticks `. For example:

Label 1`
吾家有女初长成 2024-09-20 02:58:08

你不知道。

对于您想要的空格,该格式无法满足使用df$column.1时发挥作用的标识符的要求——无法处理空格。因此,请参阅 make.names() 函数了解详细信息或示例:

> make.names(c("Foo Bar", "tic tac"))
[1] "Foo.Bar" "tic.tac"  
>                                              

十一年后编辑:答案仍然是 R 更喜欢列名可以是有效的变量名。但 R 很灵活:如果您坚持使用其他形式,但需要明确要求语言内无效的列名称:

> x <- c(1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10)
> df <- data.frame("Label 1"=x,"Label 2"=rnorm(100), check.names=FALSE)
> summary( df

你不知道。

对于您想要的空格,该格式无法满足使用df$column.1时发挥作用的标识符的要求——无法处理空格。因此,请参阅 make.names() 函数了解详细信息或示例:

> make.names(c("Foo Bar", "tic tac"))
[1] "Foo.Bar" "tic.tac"  
>                                              

十一年后编辑:答案仍然是 R 更喜欢列名可以是有效的变量名。但 R 很灵活:如果您坚持使用其他形式,但需要明确要求语言内无效的列名称:

Label 2` ) Min. 1st Qu. Median Mean 3rd Qu. Max. -2.2719 -0.7148 -0.0971 -0.0275 0.6559 2.5820 >

因此,可以说 check.names=FALSE我们覆盖默认(且合理)的检查,并通过将标识符括在反引号中,我们可以访问该列。

You don't.

With the space you desire the format would not satisfy the requirements for an identifier that come to play when you use df$column.1 -- that could not cope with a space. So see the make.names() function for details or an example:

> make.names(c("Foo Bar", "tic tac"))
[1] "Foo.Bar" "tic.tac"  
>                                              

Edit eleven years later: The answer still stands that R prefers column names can be valid variable names. But R is flexible: if you insist you can use the other form _but then need to require the not-otherwise-valid-within-the-language column names explicitly:

> x <- c(1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10)
> df <- data.frame("Label 1"=x,"Label 2"=rnorm(100), check.names=FALSE)
> summary( df

You don't.

With the space you desire the format would not satisfy the requirements for an identifier that come to play when you use df$column.1 -- that could not cope with a space. So see the make.names() function for details or an example:

> make.names(c("Foo Bar", "tic tac"))
[1] "Foo.Bar" "tic.tac"  
>                                              

Edit eleven years later: The answer still stands that R prefers column names can be valid variable names. But R is flexible: if you insist you can use the other form _but then need to require the not-otherwise-valid-within-the-language column names explicitly:

Label 2` ) Min. 1st Qu. Median Mean 3rd Qu. Max. -2.2719 -0.7148 -0.0971 -0.0275 0.6559 2.5820 >

So by saying check.names=FALSE we override the default (and sensible) check, and by wrapping the identifier in backticks we can access the column.

完美的未来在梦里 2024-09-20 02:58:08

您可以更改现有数据框名称以包含空格,即使用示例

x <- c(1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10)
df <- data.frame("Label 1"=x,"Label 2"=rnorm(100))
colnames(df) <- c("Label 1", "Label 2")
head(df, 3)

返回

  Label 1    Label 2
1       1  0.2013347
2       2  1.8823111
3       3 -0.5233811

,并且您仍然可以使用 $ 运算符访问列,您只需要使用双引号,例如

df$"Label 2"[1:3]

返回

[1]  0.2013347  1.8823111 -0.5233811

对我来说,自动转换列名似乎相当不一致data.frame 创建,但在列名更改期间不执行相同操作,但这就是 R 目前的工作方式。

You can change an existing data frames names to contain spaces ie using your example

x <- c(1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10)
df <- data.frame("Label 1"=x,"Label 2"=rnorm(100))
colnames(df) <- c("Label 1", "Label 2")
head(df, 3)

returns

  Label 1    Label 2
1       1  0.2013347
2       2  1.8823111
3       3 -0.5233811

and you can still access the columns using the $ operator, you just need to use double quotes eg

df$"Label 2"[1:3]

returns

[1]  0.2013347  1.8823111 -0.5233811

It seems rather inconsistent to me to auto-convert column names upon data.frame creation, but not to-do the same during column name alteration, but thats how R works at the moment.

糖粟与秋泊 2024-09-20 02:58:08
names(df)<-c('Label 1','Label 2)
names(df)<-c('Label 1','Label 2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文