Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将包含Excel日期的列格式化为文本_R_Dplyr_Readxl - Fatal编程技术网

R 将包含Excel日期的列格式化为文本

R 将包含Excel日期的列格式化为文本,r,dplyr,readxl,R,Dplyr,Readxl,我试图读取一个Excel文件,其中一系列列的名称为日期格式,即 |ID|Jan-21|Feb-21|Mar-21|etc| 这些由Excel保存为数字,并由 df <- readxl::read_excel("filename", sheet = "tab") 我想把它们转换成日期格式,我已经试过了 toDateString <- Vectorize(function(value) { number = as.numeric(value

我试图读取一个Excel文件,其中一系列列的名称为日期格式,即

|ID|Jan-21|Feb-21|Mar-21|etc|
这些由Excel保存为数字,并由

df <- readxl::read_excel("filename", sheet = "tab")
我想把它们转换成日期格式,我已经试过了

toDateString <- Vectorize(function(value) {
  number = as.numeric(value)
  if_else(!is.na(number) & number >= 44197 & number <= 44256)
    return(value)
  else
    return(format(number, "%b-%y")))
})

df2 <- df %>% rename_if(isDate, funs(toDateString))
toDateString=44197&number尝试以下功能:

toDateString <- function(x) {
  inds <- grepl('^\\d+$', x)
  x[inds] <- format(as.Date(as.numeric(x[inds]), origin = '1899-12-30'), '%b-%y')
  x
}

df <- data.frame(ID = 1:3, '44197' = rnorm(3), check.names = FALSE)
names(df) <- toDateString(names(df))
df
#  ID Jan-21
#1  1   0.68
#2  2  -0.32
#3  3  -1.31
toDateString
Warning messages:
1: In if (!is.na(number) & number >= 44197 & number <= 44256) return(TRUE) else return(FALSE) :
  the condition has length > 1 and only the first element will be used
2: In if (!is.na(number) & number >= 44197 & number <= 44256) return(TRUE) else return(FALSE) :
  the condition has length > 1 and only the first element will be used
toDateString <- function(x) {
  inds <- grepl('^\\d+$', x)
  x[inds] <- format(as.Date(as.numeric(x[inds]), origin = '1899-12-30'), '%b-%y')
  x
}

df <- data.frame(ID = 1:3, '44197' = rnorm(3), check.names = FALSE)
names(df) <- toDateString(names(df))
df
#  ID Jan-21
#1  1   0.68
#2  2  -0.32
#3  3  -1.31