Search code examples
rlistvectorlevels

remove R level of unlisted object


I have a variable named 'feedb' Here it is when it was called :

> feedb
[[1]]
[1] Nocoment
319 Levels:  - --- ... Variasu barang ditambah

[[2]]
[1] Mantao
319 Levels:  - --- ... Variasu barang ditambah

[[3]]
[1] Tolong takpilkan no resi pengiriman.
319 Levels:  - --- ... Variasu barang ditambah

I do not know where '319 Levels : - --- ... Variasu barang ditambah' comes from. And I do unlist of that variable

abc<-unlist(feedb)

and it still has level :

> abc
[1] Nocoment                                Mantao                              
[3] Tolong takpilkan no resi pengiriman.
319 Levels:  - --- ... Variasu barang ditambah

What is this Levels? and how to remove it? I want that abc contains only the value. i tried StringAsFactor=FALSE but it cannot be used


Solution

  • We can use droplevels to drop the unused levels or call factor again. It should have happened when a factor column or vector got subsetted, but the levels of the original vector still remains.

     feedb[] <- lapply(feedb, droplevels)
    

    Or

     feedb[] <- lapply(feedb, factor)
    

    Or convert to character

     feedb[] <- lapply(feedb, as.character)