简体   繁体   English

将rename_if谓词应用于列名

[英]apply rename_if predicate to column names

I am working with a set of excel spreadsheets which has column names which are dates. 我正在使用一组具有日期的列名称的Excel电子表格。

After reading in the data with readxl::read_xlsx() , these column names become excel index dates (ie an integer representing days elapsed from 1899-12-30 ) 在使用readxl::read_xlsx()读取数据后,这些列名称将成为excel索引日期(即表示从1899-12-30开始经过的天数的整数)

Is it possible to used dplyr::rename_if() or similar to rename all column names that are currently integers? 是否可以使用dplyr::rename_if()或类似名称重命名当前为整数的所有列名? I have written a function rename_func that I would like to apply to all such columns. 我编写了一个函数rename_func ,希望将其应用于所有此类列。

df %>% rename_if(is.numeric, rename_func) is not suitable as is.numeric is applied to the data in the column not the column name itself. df %>% rename_if(is.numeric, rename_func)不适合,因为is.numeric应用于列中的数据,而不是列名本身。 I have also tried: 我也尝试过:

is.name.numeric <- function(x) is.numeric(names(x))
df %>% rename_if(is.name.numeric, rename_func)

which does not work and does not change any names (ie is.name.numeric returns FALSE for all cols) 这不起作用并且不更改任何名称(即is.name.numeric对所有is.name.numeric返回FALSE

edit: here is a dummy version of my data 编辑:这是我的数据的虚拟版本

df_badnames <- structure(list(Level = c(1, 2, 3, 3, 3), Title = c("AUSTRALIAN TOTAL", 
"MANAGERS", "Chief Executives, Managing Directors & Legislators", 
"Farmers and Farm Managers", "Hospitality, Retail and Service Managers"
), `38718` = c(213777.89, 20997.52, 501.81, 121.26, 4402.7), 
    `38749` = c(216274.12, 21316.05, 498.1, 119.3, 4468.67), 
    `38777` = c(218563.95, 21671.84, 494.08, 118.03, 4541.02), 
    `38808` = c(220065.05, 22011.76, 488.56, 116.24, 4609.28)), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame"))

and I would like: 我想:

df_goodnames <- structure(list(Level = c(1, 2, 3, 3, 3), Title = c("AUSTRALIAN TOTAL", 
"MANAGERS", "Chief Executives, Managing Directors & Legislators", 
"Farmers and Farm Managers", "Hospitality, Retail and Service Managers"
), Jan2006 = c(213777.89, 20997.52, 501.81, 121.26, 4402.7), 
    Feb2006 = c(216274.12, 21316.05, 498.1, 119.3, 4468.67), 
    Mar2006 = c(218563.95, 21671.84, 494.08, 118.03, 4541.02), 
    Apr2006 = c(220065.05, 22011.76, 488.56, 116.24, 4609.28)), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame"))

I understand that it is best practice to create a date column and change the shape of this df, but I need to join a few spreadsheets first and having integer column names causes a lot of problems. 我知道创建date列并更改此df的形状是最佳实践,但是我需要先加入一些电子表格,并且使用整数列名会带来很多问题。 I currently have a work around but the crux of my question (apply a rename_if predicate to a name, rather than a column) is still interesting. 我目前有一个解决方法,但是问题的症结(将rename_if谓词应用于名称,而不是列)仍然很有趣。

Although, the names look numeric but they are not 虽然名称看起来是数字,但不是

class(names(df_badnames))
#[1] "character"

so they would not be caught by is.numeric or similar other functions. 因此它们不会被is.numeric或类似的其他函数捕获。

One way to do this is find out which names can be coerced to numeric and then convert them into the date format of our choice 一种方法是找出可以强制转换为数字的names ,然后将其转换为我们选择的日期格式

cols <- as.numeric(names(df_badnames))
names(df_badnames)[!is.na(cols)] <- format(as.Date(cols[!is.na(cols)], 
                                          origin = "1899-12-30"), "%b%Y")

df_badnames

#  Level Title                                           Jan2006 Feb2006 Mar2006 Apr2006
#   <dbl> <chr>                                             <dbl>   <dbl>   <dbl>   <dbl>
#1     1 AUSTRALIAN TOTAL                                213778. 216274. 218564. 220065.
#2     2 MANAGERS                                         20998.  21316.  21672.  22012.
#3     3 Chief Executives, Managing Directors & Legisla…    502.    498.    494.    489.
#4     3 Farmers and Farm Managers                          121.    119.    118.    116.
#5     3 Hospitality, Retail and Service Managers          4403.   4469.   4541.   4609.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM