0

In R there are a lot of functions which you can use in the "as."-version. I dont understand the difference. Example:

a <- c("a", "a", "a", "b", "b", "c")
b <- factor(a)
c <- as.factor(a)
identical(b, c)

identical(b, c) will get TRUE. So at least in this case is no difference between as.factor and factor, both of them convert the character-vector a to a factor. There are lots of similar examples (as.data.frame and data.frame, as.numeric and numeric, as.vector and vector, ...). Maybe somebody can explain the general difference of use between these.

joel.wilson
  • 8,243
  • 5
  • 28
  • 48
  • 1
    You can specify `levels`, `labels` etc in `factor` when compared to `as.factor` i.e. `b <- factor(a, levels = letters[1:4]); c <- as.factor(a, levels = letters[1:4])# Error in as.factor(a, levels = letters[1:4]) : unused argument (levels = letters[1:4])` – akrun Dec 26 '16 at 17:49
  • 1
    `as.*` is for converting one object to another and functions without the `as.` part are for constructing objects from scratch. – Roman Luštrik Dec 26 '16 at 17:49

0 Answers0