R语言 创建新变量而不复制粘贴旧变量名称

3pvhb19x  于 2023-02-01  发布在  其他
关注(0)|答案(1)|浏览(172)

有时重命名变量时,变量名可能很长,例如,如果它来自使用excel导出的问卷,其中变量名是整个问题。
这些可能是令人沮丧的处理,他们可能不完全复制,你可能不正确复制等。
下面是我处理这个问题的方法。请随意评论一个更好的方法:

library(tidyverse) 
   
data <- data.frame( "please tell us what your age is?" = c(24, 35, 46, 14 ),
            "what is your sex?" = c("male", "female", "male", "female"))

data

# create an "age" var and a "sex" var

data <- data %>% rename("age" = names(data)[1])
data <- data %>% rename("sex" = names(data)[2])

data

这使得变量的重命名达到更难出错的数字,以及你想要的变量名。
有没有人可以对此进行改进?比如迭代?

lsmd5eda

lsmd5eda1#

您可以使用 tibble 包中tibble函数的参数**.name_repair**,它有不同的选项,允许您处理有问题的列名,例如下面的选项。(* 注意:* 在这些示例中,我添加了一些新列,只是为了注意不同之处。)

data <-
  data.frame(
    "please tell us what your age is?" = c(24, 35, 46, 14),
    "what is your sex?" = c("male", "female", "male", "female"),
    "what is your sex?" = c(1, 0, 1, 0),
    x = c(0, 1, 2, 3),
    x = c(8, 90, 23, 45),
    check.names = FALSE
  )

*最低限度:没有名义的修理或检查,超越了基本的存在。

> tibble(data, .name_repair = "minimal")
> tibble(data, .name_repair = "minimal")
# A tibble: 4 × 5
  `please tell us what your age is?` `what is your sex?` `what is your sex?`     x     x
                               <dbl> <chr>                             <dbl> <dbl> <dbl>
1                                 24 male                                  1     0     8
2                                 35 female                                0     1    90
3                                 46 male                                  1     2    23
4                                 14 female                                0     3    45

*unique:确保名称唯一且不为空。

> tibble(data, .name_repair = "unique")
New names:
• `what is your sex?` -> `what is your sex?...2`
• `what is your sex?` -> `what is your sex?...3`
• `x` -> `x...4`
• `x` -> `x...5`
# A tibble: 4 × 5
  `please tell us what your age is?` `what is your sex?...2` `what is your sex?...3` x...4 x...5
                               <dbl> <chr>                                     <dbl> <dbl> <dbl>
1                                 24 male                                          1     0     8
2                                 35 female                                        0     1    90
3                                 46 male                                          1     2    23
4                                 14 female                                        0     3    45

*检查唯一性:(默认值),不进行名称修复,但检查它们是否唯一,

> tibble(data, .name_repair = "check_unique")

Error:
! Column names `what is your sex?` and `x` must not be duplicated.
Use .name_repair to specify repair.
Caused by error in `repaired_names()`:
! Names must be unique.
✖ These names are duplicated:
  * "what is your sex?" at locations 2 and 3.
  * "x" at locations 4 and 5.
Run `rlang::last_error()` to see where the error occurred.

*普遍:使名称唯一且符合语法

> tibble(data, .name_repair = "universal")
New names:
• `please tell us what your age is?` -> `please.tell.us.what.your.age.is.`
• `what is your sex?` -> `what.is.your.sex....2`
• `what is your sex?` -> `what.is.your.sex....3`
• `x` -> `x...4`
• `x` -> `x...5`
# A tibble: 4 × 5
  please.tell.us.what.your.age.is. what.is.your.sex....2 what.is.your.sex....3 x...4 x...5
                             <dbl> <chr>                                 <dbl> <dbl> <dbl>
1                               24 male                                      1     0     8
2                               35 female                                    0     1    90
3                               46 male                                      1     2    23
4                               14 female                                    0     3    45
  • ...

有关详细信息,请参见help(tibble::tibble)

相关问题