Search code examples
rvectordataframetype-conversionelement

Converting a variable stored in a list into a list of character vectors in r


I have a subset of data that originates from a very large dataset. I have split this subset of data into a list of dataframes so that each case/id is a separate element within the list. Each element is named with the case/id. I then remove all variables from each dataframe element to be left with only one variable - called 'state'. It is currently a factor with 7 levels.

I am attempting to turn this list of 'state' elements into a list of character vectors. The element below is the first in the list, and included are the row numbers (which originate from the much larger original dataset).

[[1]]
        state
104246 active
104247   rest
104248 active
104249 active
.
.
.
104315 active
104316 active
104317   rest
104318   rest

I am trying to turn this simply into a character vector that would look like this:

[1] "active" "rest" "active" "active" ........... "active" "active" "rest" "rest"

It seems simple. I have tried doing things like (where 'temp' is the list name):

as.vector(as.matrix(temp))   

This returns something like this:

         [,1]  
    id1  List,1
    id2  List,1
    id3  List,1
    id4  List,1

When I look at each element from this they basically appear to be still in longform.

Alternatively, I tried directly converting to a character:

as.vector(as.character(temp))

But, this comes back as not the ideal format (though, I guess I could hack this to convert the factor level numbers to words... (note in the large dataset, there are 7 levels of the factor 'state')

[1] "list(state = c(1, 4, 1, 1, 1, 1, 1, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 1, 6, 1, 4, 4, 1, 1, 1, 4,     1, 1, 1, 6, 4, 1, 1, 1, 1, 1, 4, 4, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 1, 1, 1, 1, 4, 4, 1, 1, 1, 1,     1, 1, 1, 4, 4))"

I also tried making the variable 'state' which is a factor a character variable prior to conversion, but that didn't help.

Here is the data for a reproducible example. It contains two elements in the list 'temp' only in this example:

temp<-list(structure(list(state = structure(c(1L, 4L, 1L, 1L, 1L, 1L, 
                                           1L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 1L, 
                                           6L, 1L, 4L, 4L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 6L, 4L, 1L, 1L, 1L, 
                                           1L, 1L, 4L, 4L, 1L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                           4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                           1L, 4L, 4L), .Label = c("active", "active2", "active3", "rest", "rest2", 
                                                                   "stop", "stop2"), class = "factor")), .Names = "state", row.names = 104246:104318, class = "data.frame"), 
        structure(list(state = structure(c(1L, 4L, 4L, 4L, 1L, 1L, 
                                           1L, 4L, 4L, 4L, 4L, 1L, 4L, 4L, 4L, 1L, 1L, 6L, 4L, 1L, 4L, 
                                           4L, 4L, 1L, 4L, 1L, 1L, 1L), .Label = c("active", "active2", 
                                                                                   "active3", "rest", "rest2", "stop", "stop2"), class = "factor")), .Names = "state", row.names = 950:977, class = "data.frame"))



str(temp)

Solution

  • L = lapply(temp, function(x) as.character(unlist(x))) Just L[[1]] or L[[2]] for the vectors.