'max'对R中的因素没有意义

Sim*_*aur 2 r

getImportDate <- function(){
  hdfs.init()
  f <- hdfs.file("/user/rstudio/rdataecommerce4.csv","r")
  m <- hdfs.read(f)
  c <- rawToChar(m)
  data <- read.table(textConnection(c), header=TRUE , sep = ",")
  print(max(data$date, na.rm=TRUE))  //ERROR AT THIS LINE

}
Run Code Online (Sandbox Code Playgroud)

我收到错误:

Error in Summary.factor(c(49L, 49L, 49L, 49L, 68L, 69L, 71L, 72L, 74L,  : ‘max’ not meaningful for factors
Run Code Online (Sandbox Code Playgroud)

为什么我会收到此错误?如何解决此问题?

luk*_*keA 12

为什么我收到此错误?

你得到这个错误是因为试图获得类型因子变量的最大值,这没有意义.

max(as.factor(49L))
# Error in Summary.factor(1L, na.rm = FALSE) : 
#   ‘max’ not meaningful for factors
Run Code Online (Sandbox Code Playgroud)

我该如何解决?

使用变量(可能在类型转换后)max是有意义的:

max(as.Date("2016-01-01"))
# [1] "2016-01-01"
Run Code Online (Sandbox Code Playgroud)