-3

I want to change the data type to factor. The data shown in the example below has only three variables. So, if you want to change the data type to factor, you can type code 3times like this.

>str(zxc2)
 'data.frame':  50000 obs. of  3 variables:
 $ CA0000005: int  1 1 2 2 1 1 2 1 5 1 ...
 $ CA0000008: int  1 0 2 2 1 1 2 1 1 0 ...
 $ CA0000602: int  0 832 3077 3155 0 0 0 0 995 2712 ...

>zxc2$CA0000005<-as.factor(zxc2$CA0000005)
>zxc2$CA0000008<-as.factor(zxc2$CA0000008)
>zxc2$CA0000602<-as.factor(zxc2$CA0000602)

However, if there are a lot of variables, it is hard to input each one as above. If all variables need to be changed to factor, how can i change them at once?

Thanks in advance for your reply.

서영재
  • 96
  • 1
  • 9

1 Answers1

1

We can use lapply to loop over the columns, convert it to factor and assign it back to the original dataset

zxc2[] <- lapply(zxc2, factor)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • I have no clue about the downvotes, but variations of this are the correct answer. Perhaps a `sapply` for easiness. – catastrophic-failure Jan 04 '17 at 15:39
  • @catastrophic-failure The `sapply` method is not good as it converts it to a `matrix` and matrix can hold only a single class, resulting in `character`, but wrapping it with `data.frame` again converts to `factor`, but that is a lot more things happening for a simple conversion. – akrun Jan 04 '17 at 15:55
  • 1
    True, I'm so accustomed to using `character` that I always end using `matrix` instead of `data.frame`, so I never miss the factors. – catastrophic-failure Jan 04 '17 at 15:59
  • @Then... If dataset have character variable, what function must be used? as your reply, I can't use >as.data.frame(sapply(zxc2, factor)). right? – 서영재 Jan 05 '17 at 02:43
  • 1
    @서영재 What I meant is that it is roundabout way of doing the same thing as `as.data.frame(as.matrix(zxc2))`, It is better to use the option in my answer – akrun Jan 05 '17 at 02:45