0

The output when checking an empty directory with R's list.files() is a character string...

> list.files('/home/someDir')
character(0)

But I would like to test its length and use it in a control structure like

> current <- '/home/someDir'
> dirPick <- function(){
    tryCatch(
      if (length(list.files(current) > 0)) {
        dirData <- current
      } else {
        dirData <- old
      }
    )
  }

But the object returned by list.files() doesn't act like I'd expect:

> list.files('/home/someDir') == 0
logical(0)


> as.numeric(list.files('/home/someDir')) == 0
logical(0)

Not sure I understand the difference between a logical and boolean, but why doesn't this just act like an integer zero?

d8aninja
  • 3,233
  • 4
  • 36
  • 60

1 Answers1

2

Try:

length(list.files('/home/someDir')) == 0

The value character(0) indicates some like an empty variable. Thats why you can't sum or convert it into 0 without length() attribute

gonzalez.ivan90
  • 1,322
  • 1
  • 12
  • 22