3

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

user2187202
  • 337
  • 1
  • 3
  • 11
  • There is no list in your question. `firstColumn` is a vector. In your own interest you should learn proper terminology. – Roland May 27 '13 at 10:47

3 Answers3

6

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])
juba
  • 47,631
  • 14
  • 113
  • 118
4

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

Try

as.character(firstColumn[[1]])
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
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)
Johan Zicola
  • 1,021
  • 8
  • 6