【问题标题】:Why R Changes Special Characters In Column Name To Dots为什么 R 将列名中的特殊字符更改为点
【发布时间】:2017-09-26 23:30:32
【问题描述】:

我有一个更大的数据集,当我导入到 R 时,它的列名带有 , 和不同类型的特殊字符。

当我将此数据集用作另一个变量中的副本或作为另一个较小数据的子集或使用来自同一较大数据集的数据和列名执行不同类型的数据转换时,列名中的所有特殊字符都会更改为..

有没有办法在列名中保留特殊字符?我不希望 R 更改与列名相关的任何内容。

请提出解决方案。

示例

> library(MASS)
> data(cats)
> cats <- cats[1:10,]
> cats
   Sex Bwt Hwt
1    F 2.0 7.0
2    F 2.0 7.4
3    F 2.0 9.5
4    F 2.1 7.2
5    F 2.1 7.3
6    F 2.1 7.6
7    F 2.1 8.1
8    F 2.1 8.2
9    F 2.1 8.3
10   F 2.1 8.5
> colnames(cats) <- c("A:17272,,1,MPR.rtn_rslt", "B:17272,,1,MPR.rtn_rslt", "C:17272,,1,MPR.rtn_rslt")
> cats
   A:17272,,1,MPR.rtn_rslt B:17272,,1,MPR.rtn_rslt C:17272,,1,MPR.rtn_rslt
1                        F                     2.0                     7.0
2                        F                     2.0                     7.4
3                        F                     2.0                     9.5
4                        F                     2.1                     7.2
5                        F                     2.1                     7.3
6                        F                     2.1                     7.6
7                        F                     2.1                     8.1
8                        F                     2.1                     8.2
9                        F                     2.1                     8.3
10                       F                     2.1                     8.5

cats 数据集的列名带有特殊字符 ,:。下面,我正在执行数据转换。

> # Define the avector-subselection method
> as.data.frame.avector <- as.data.frame.vector
> `[.avector` <- function(x,i,...) {
+   r <- NextMethod("[")
+   mostattributes(r) <- attributes(x)
+   r
+ }

> # Preserve attributes as they are lost due to subet
> test <- data.frame(
+   lapply(cats, function(x) {
+     structure( x, class = c("avector", class(x) ) )
+   } )
+ )

> test
   A.17272..1.MPR.rtn_rslt B.17272..1.MPR.rtn_rslt C.17272..1.MPR.rtn_rslt
1                        F                     2.0                     7.0
2                        F                     2.0                     7.4
3                        F                     2.0                     9.5
4                        F                     2.1                     7.2
5                        F                     2.1                     7.3
6                        F                     2.1                     7.6
7                        F                     2.1                     8.1
8                        F                     2.1                     8.2
9                        F                     2.1                     8.3
10                       F                     2.1                     8.5

上述转换导致新数据test 来自cats,将所有特殊字符如:, 更改为.

【问题讨论】:

    标签: r columnname


    【解决方案1】:

    试试:

    test <- data.frame(lapply(cats, function(x) {
            structure(x, class = c("avector", class(x)))
    }), check.names = FALSE)
    

    您必须使用引号或在某些情况下使用格式反引号来引用名称,但最好重命名整个数据框。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-09
      • 1970-01-01
      • 2017-12-20
      • 2021-12-09
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多