【问题标题】:How i can rename all columns of a tibble in R with the same name using dplyr?如何使用 dplyr 重命名 R 中同名的 tibble 的所有列?
【发布时间】:2022-08-17 18:22:15
【问题描述】:

我在 R 中有一个数据框,所有奇数列都没有名称,它们都包含日期,但格式不同(从 excel 文件导入),如下所示:

> data
# A tibble: 557 x 24
   ...1                     x1    ...3                  x2   ...5     x3  ...7                     x4  ...9
   <dttm>                  <dbl> <dttm>                <dbl> <dbl>   <dbl> <dttm>                <dbl> <dbl>
 1 2013-06-13 00:00:00      26.3 2013-02-07 00:00:00    26.6 41312    26.4 2015-06-01 00:00:00    21.4 42156
 2 2013-06-20 00:00:00      26.6 2013-02-08 00:00:00    26.9 41313    26.6 2015-06-02 00:00:00    21.3 42157
 3 2013-10-28 00:00:00      26.2 2013-02-11 00:00:00    26.6 41316    26.4 2015-06-03 00:00:00    21.3 42158
 4 NA                       NA   2013-02-12 00:00:00    26.6 41317    26.4 2015-06-04 00:00:00    21.2 42159
 5 NA                       NA   2013-02-13 00:00:00    26.5 41318    26.3 2015-06-05 00:00:00    21.3 42160
 6 NA                       NA   2013-02-14 00:00:00    26.6 41319    26.4 2015-06-08 00:00:00    21.4 42165
 7 NA                       NA   2013-02-15 00:00:00    26.4 41320    26.2 2015-06-10 00:00:00    21.5 42166
 8 NA                       NA   2013-02-18 00:00:00    26.4 41323    26.2 2015-06-11 00:00:00    21.3 42167
 9 NA                       NA   2013-02-19 00:00:00    26.3 41324    26.1 2015-06-12 00:00:00    21.3 42170
10 NA                       NA   2013-02-20 00:00:00    26.2 41325    26.0 2015-06-15 00:00:00    21.2 42171

我想将所有没有名称的列重命名为“日期”,使其具有相同的日期格式(as.Date())。

我找到了一个方法 :

colnames(data)[seq(1:1000) %% 2 !=0] <- \"date\"

但我想用 dplyr 包的 rename() 函数重命名它们,最后用日期问题格式化它们。我如何使用 dplyr 在 R 中做到这一点?

有什么帮助吗?

  • 恐怕rename(或rename_with)在这种情况下不会允许您拥有超过1个具有相同名称的列。

标签: r datetime dplyr


【解决方案1】:

你想要这样的东西吗? (名称必须是唯一的)

library(dplyr)

df %>%
  rename_with(~stringr::str_replace(.,"...","date" ), .cols = contains("...")) %>% 
  mutate(across(starts_with("date"), ~lubridate::as_date(.)))

输出:

        date1   x1      date3   x2      date5   x3      date7   x4      date9
1  2013-06-13 26.3 2013-02-07 26.6 2083-02-09 26.4 2015-06-01 21.4 2085-06-02
2  2013-06-20 26.6 2013-02-08 26.9 2083-02-10 26.6 2015-06-02 21.3 2085-06-03
3  2013-10-28 26.2 2013-02-11 26.6 2083-02-13 26.4 2015-06-03 21.3 2085-06-04
4        <NA>   NA 2013-02-12 26.6 2083-02-14 26.4 2015-06-04 21.2 2085-06-05
5        <NA>   NA 2013-02-13 26.5 2083-02-15 26.3 2015-06-05 21.3 2085-06-06
6        <NA>   NA 2013-02-14 26.6 2083-02-16 26.4 2015-06-08 21.4 2085-06-11
7        <NA>   NA 2013-02-15 26.4 2083-02-17 26.2 2015-06-10 21.5 2085-06-12
8        <NA>   NA 2013-02-18 26.4 2083-02-20 26.2 2015-06-11 21.3 2085-06-13
9        <NA>   NA 2013-02-19 26.3 2083-02-21 26.1 2015-06-12 21.3 2085-06-16
10       <NA>   NA 2013-02-20 26.2 2083-02-22 26.0 2015-06-15 21.2 2085-06-17

【讨论】:

  • 它在每列日期中添加一个数字,格式也不起作用。它再现了 2085 年。我希望所有日期列都具有相同的名称“日期”。
  • 您希望所有日期列都称为“日期”吗?这是不可能的,它们必须是独一无二的。
猜你喜欢
  • 1970-01-01
  • 2018-05-29
  • 1970-01-01
  • 2020-05-27
  • 2018-05-13
  • 2017-09-20
  • 2020-02-02
  • 2023-01-10
  • 1970-01-01
相关资源
最近更新 更多