【问题标题】:How to rename a dataframe with a list [duplicate]如何用列表重命名数据框[重复]
【发布时间】:2019-09-27 15:31:09
【问题描述】:

我有这个数据集:

library(tidyverse)
DF <- tribble(
    ~District,        ~Income,
    "District 1",        1610,
    "district2",         4906,
    "DISTRICT1",        12082,
    "DISTRICT 1",       13791,
    "district1",        21551,
    "District 2",       35126,
)

DF

我有一个包含新变量名称的列表(在现实生活中,我有很多变量)。

Nombres <- list(c("Distrito" , "Ingreso"))

我想重命名数据集,我的预期结果是:

# A tibble: 6 x 2
  Distrito   Ingreso
  <chr>        <dbl>
1 District 1    1610
2 district2     4906
3 DISTRICT1    12082
4 DISTRICT 1   13791
5 district1    21551
6 District 2   35126

非常感谢您的帮助。问候!

【问题讨论】:

  • 你有没有碰巧看到this

标签: r list dataframe rename


【解决方案1】:

这可能是 cmets 中提到的问题的重复 但一条线的解决方案是:

colnams(F) = unlist(Nombre)

你会得到:

# A tibble: 6 x 2
  Distrito   Ingreso
  <chr>        <dbl>
1 District 1    1610
2 district2     4906
3 DISTRICT1    12082
4 DISTRICT 1   13791
5 district1    21551
6 District 2   35126

请注意,不取消列出名称将为您提供以下信息:

# A tibble: 6 x 2
  'c("Distrito","Ingreso")' NA  
  <chr>                    <dbl>
1 District 1                1610
2 district2                 4906
3 DISTRICT1                 12082
4 DISTRICT 1                13791
5 district1                 21551
6 District 2                35126

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2018-04-02
    • 2020-04-28
    • 2018-04-29
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    相关资源
    最近更新 更多