在if(is.na(x))中,{:条件具有长度,并且在R中只使用第一个元素
我试图将字符串转换为列表中特定df中特定列的日期数据类; 由于它们是excel格式,我目前正在使用在if(is.na(x))中,{:条件具有长度,并且在R中只使用第一个元素,r,string,list,date,lapply,R,String,List,Date,Lapply,我试图将字符串转换为列表中特定df中特定列的日期数据类; 由于它们是excel格式,我目前正在使用excel\u numeric\u to\u datefromguantitor包来转换它们 然而,当我应用下一个代码时,我遇到了很多警告,并且输出返回NA而不是日期 date_columns <- c("date1", "date2", "date3", "date4") fun_excel_date &l
excel\u numeric\u to\u date
fromguantitor
包来转换它们
然而,当我应用下一个代码时,我遇到了很多警告,并且输出返回NA而不是日期
date_columns <- c("date1", "date2", "date3", "date4")
fun_excel_date <- function(x){
if(is.na(x)){
return(NA)
} else {
excel_numeric_to_date(as.numeric(
as.character(x)
), date_system = "modern")
}
}
df_list[["input_1"]][date_columns] <- lapply(
df_list[["input_1"]][date_columns],
fun_excel_date
)
是否有其他方法应用此函数?要传递给
if
条件的变量必须仅包含1个元素。例如if(3>2)
而不是if(3>1:2)
。因此在您的情况下,x
是一个向量,是.na(x)
返回一个向量,但是如果不接受向量,那么警告为什么你甚至需要这个条件块?看门人::excel\u numeric\u to_date()
已经可以很好地处理NA值。>看门人::excel\u numeric\u to_date(c(40000,40001,NA,40002),date\u system=“modern”)
返回[1]“2009-07-06”“2009-07-07”不适用“2009-07-08”
Warning messages:
1: In if (is.na(x)) { :
la condición tiene longitud > 1 y sólo el primer elemento será usado
2: In if (is.na(x)) { :
la condición tiene longitud > 1 y sólo el primer elemento será usado
3: In if (is.na(x)) { :
la condición tiene longitud > 1 y sólo el primer elemento será usado
4: In if (is.na(x)) { :
la condición tiene longitud > 1 y sólo el primer elemento será usado
5: In if (is.na(x)) { :
la condición tiene longitud > 1 y sólo el primer elemento será usado
6: In if (is.na(x)) { :
la condición tiene longitud > 1 y sólo el primer elemento será usado
7: In if (is.na(x)) { :
la condición tiene longitud > 1 y sólo el primer elemento será usado
8: In if (is.na(x)) { :
la condición tiene longitud > 1 y sólo el primer elemento será usado