重命名存在于 data.frame 中的列,同时保持大小写

Rename columns existing in data.frame while maintaining case

我有两个data.frames:

df <- data.frame(Cats = c(1,2,3), Dogs = c(1,2,3), Bears = c(1,2,3))

df_lower <- structure(list(cats = c(1, 2, 3), n = c(1, 1, 1), percent = c(0.333333333333333, 
0.333333333333333, 0.333333333333333)), class = "data.frame", row.names = c(NA, 
-3L), core = structure(list(cats = c(1, 2, 3), n = c(1, 1, 1), 
    percent = c(0.333333333333333, 0.333333333333333, 0.333333333333333
    )), class = "data.frame", row.names = c(NA, -3L)), tabyl_type = "one_way")

df:

  Cats Dogs Bears
1    1    1     1
2    2    2     2
3    3    3     3

df_lower:

  cats n   percent
1    1 1 0.3333333
2    2 1 0.3333333
3    3 1 0.3333333

如何通过检测 df 中存在的名称然后匹配它们来重命名 df_lower 中的列?

预期输出:

  Cats n   percent
1    1 1 0.3333333
2    2 1 0.3333333
3    3 1 0.3333333

我们将第一个的列名转换为小写,然后做一个match

i1 <- match(names(df_lower), tolower(names(df)), nomatch = 0)
i2 <- i1 > 0
nm1 <- names(df_lower)[i2]
df_lower <- df_lower %>%
    rename_at(vars(nm1), ~  names(df)[i1[i2]])

-输出

df_lower
#   Cats n   percent
#1    1 1 0.3333333
#2    2 1 0.3333333
#3    3 1 0.3333333

或使用base R

names(df_lower)[i2] <- names(df)[i1[i2]]
df <- data.frame(
  Cats = 1:3,
  Dogs = 1:3,
  Bears = 1:3
)

df_lower <- data.frame(
  cats = 1:3,
  n = rep(1, 3),
  percent = rep(1/3, 3)
)

# Is this what you wanted?
df_lower <- janitor::as_tabyl(df_lower)

# Get column names
cn1 <- colnames(df)
cn2 <- colnames(df_lower)

# Returns the index of cn2 for each cn1 match
# cn2 is already all lowercase
ind <- match(cn2, tolower(cn1), nomatch = 0L)
# assign new colum names
colnames(df_lower)[ind > 0] <- cn1[ind]

df
#>   Cats Dogs Bears
#> 1    1    1     1
#> 2    2    2     2
#> 3    3    3     3
df_lower
#>  Cats n   percent
#>     1 1 0.3333333
#>     2 1 0.3333333
#>     3 1 0.3333333

reprex package (v1.0.0)

于 2021-02-18 创建