0

I want to initialise a data frame such that it contains a variety of columns, some are character vectors but not factors, while other are factors.

In such a case, I cannot use stringsAsFactors because that would set all columns to be factors or characters depending on its value.

What can I do?

ag14
  • 867
  • 1
  • 8
  • 15
  • My suggestion is to work with character columns until you perform an analysis or maybe do some plotting. character columns are typically easier to work with. If you really want factors, then follow Frank's template. – lmo Apr 11 '17 at 17:03
  • 2
    `data.frame(a = 1, b = "bah", c = factor("caw", levels=c("caw","yaw","naw")), stringsAsFactors = FALSE)` works fine for me. If by "empty" you mean having no rows... that's a bad idea in R. – Frank Apr 11 '17 at 17:05
  • Possibly useful: http://stackoverflow.com/questions/24621280/r-i-set-stringsasfactors-f-but-still-get-an-invalid-factor-level-na-genera – MrFlick Apr 11 '17 at 18:32
  • Can anyoen help me with why this question was downvoted? I would like to avoid making the same mistakes going forward. – ag14 Apr 13 '17 at 11:32

1 Answers1

0

It is not clear what you mean by "empty" data frame. But you can initialize data frame without any rows but with clear define column and column types as follows.

empty_df <- data.frame(a = numeric(),
                       b =  character(),
                       c = logical(),
                       d = integer(), 
                       stringsAsFactors = FALSE)

str(empty_df)
#'data.frame':  0 obs. of  4 variables:
#$ a: num 
#$ b: chr 
#$ c: logi 
#$ d: int 

You can set stringsAsFactors = FALSE first and then convert column to facter, as others suggested.

www
  • 38,575
  • 12
  • 48
  • 84