0

mydf is for reproducible purpose . I have mydf data frame , and I want to convert list as factors in mydf , but it throws an error

mydf<-data.frame(col1=c("a","b"),col2=c("f","j"))
mydf$col1<-as.list(mydf$col1)
mydf$col2<-as.list(mydf$col2)
str(mydf)

This is the error I get when I try to change lists to factors/numeric type

mydf$col1<-as.factor(mydf$col1)

Error in order(y) : unimplemented type 'list' in 'orderVector1'

I want my data frame (mydf) to be expected_df (no lists data frame)

expected_df<-data.frame(col1=c("a","b"),col2=c("f","j"))
str(expected_df)

If you compared str(mydf) and str(expected_df) , there is a difference as I am unable to change lists to factors in mydf data frame. Is there any workaround to solve my issue ?

str(mydf)
'data.frame':   2 obs. of  2 variables:
$ col1:List of 2
..$ : Factor w/ 2 levels "a","b": 1
..$ : Factor w/ 2 levels "a","b": 2
$ col2:List of 2
..$ : Factor w/ 2 levels "f","j": 1
..$ : Factor w/ 2 levels "f","j": 2



str(expected_df)
'data.frame':   2 obs. of  2 variables:
$ col1: Factor w/ 2 levels "a","b": 1 2
$ col2: Factor w/ 2 levels "f","j": 1 2
Yogesh Kumar
  • 609
  • 6
  • 22
  • 1
    `mydf[] <- lapply(mydf, unlist)`. – Rui Barradas Mar 02 '19 at 10:34
  • I have about 2000+ files with text data , few json files have 20 columns , others have 18 columns [remaining 2 columns are not there ]...so this is also an issue ....I used your code , but it threw some error Error in `[<-.data.frame`(`*tmp*`, , value = list(bwithbug = c("true", : replacement element 10 has 2 rows, need 3 – Yogesh Kumar Mar 02 '19 at 11:44
  • If your problem is JSON, read [this](https://stackoverflow.com/questions/2617600/importing-data-from-a-json-file-into-r#2617823). – Rui Barradas Mar 02 '19 at 12:11

2 Answers2

0

You can use stringsAsFactors = TRUE

> mydf <- data.frame(col1 = c("a", "b"), col2 = c("f", "j"), stringsAsFactors = TRUE)
> mydf
  col1 col2
1    a    f
2    b    j
> mydf$col1
[1] a b
Levels: a b
> str(mydf)
'data.frame':   2 obs. of  2 variables:
$ col1: Factor w/ 2 levels "a","b": 1 2
$ col2: Factor w/ 2 levels "f","j": 1 2
Sonny
  • 3,083
  • 1
  • 11
  • 19
  • mydf is only for reproducible purpose here ...Actual data is huge , and I am not creating that df , and it is provided only so , i cannot use this strimngsasfactors – Yogesh Kumar Mar 02 '19 at 11:35
  • Assume that you are given mydf column . How would u convert lists in your data frame to factor ? Thats my question ...Your answer did not cover anything related to lists given in data frame !! – Yogesh Kumar Mar 02 '19 at 11:37
0

Late to the party here, but I thought I would share my experience for future searches. I was also having the 'Error in order(y)' error when trying to convert a column to factors. The way I got round it was to explicitly label the factors. In your example it would be like so:

# instead of this:
# mydf$col1 <- as.factor(mydf$col1)

# using this:
mydf$col1 <- factor(mydf$col1, levels=c("a","b"))