Question

I have a data frame like this

    df <- data.frame(letters=letters[1:5], numbers=seq(1:5))

and lets say that I want to extram the first column into a list

firstColumn <- df[,1]

>  firstColumn[[1]]
[1] a
Levels: a b c d e

Problème is I want to remove the level to have a string

any help please ?

thanks

Was it helpful?

Solution 2

If I understand your question, you're trying to convert to character.

Try

as.character(firstColumn[[1]])

OTHER TIPS

Either define your variable as character from the beginning :

df <- data.frame(letters=letters[1:5], numbers=seq(1:5), stringsAsFactors=FALSE)

Or convert it afterwards :

firstColumn <- as.character(df[,1])

If you want a list but without levels, first import the column of your dataframe as a vector and then turn it into a list. Here the one-liner:

firstColumn_list <- as.list(as.vector(df[,1]))

You could also use the drop.levels function from the package gdata:

firstColumn_list <- as.list(df[,1])
firstColumn_list_wo_levels <- gdata::drop.levels(firstColumn_list)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top