Convert data.frame column to a vector?

479,692

Solution 1

I'm going to attempt to explain this without making any mistakes, but I'm betting this will attract a clarification or two in the comments.

A data frame is a list. When you subset a data frame using the name of a column and [, what you're getting is a sublist (or a sub data frame). If you want the actual atomic column, you could use [[, or somewhat confusingly (to me) you could do aframe[,2] which returns a vector, not a sublist.

So try running this sequence and maybe things will be clearer:

avector <- as.vector(aframe['a2'])
class(avector) 

avector <- aframe[['a2']]
class(avector)

avector <- aframe[,2]
class(avector)

Solution 2

There's now an easy way to do this using dplyr.

dplyr::pull(aframe, a2)

Solution 3

You could use $ extraction:

class(aframe$a1)
[1] "numeric"

or the double square bracket:

class(aframe[["a1"]])
[1] "numeric"

Solution 4

You do not need as.vector(), but you do need correct indexing: avector <- aframe[ , "a2"]

The one other thing to be aware of is the drop=FALSE option to [:

R> aframe <- data.frame(a1=c1:5, a2=6:10, a3=11:15)
R> aframe
  a1 a2 a3
1  1  6 11
2  2  7 12
3  3  8 13
4  4  9 14
5  5 10 15
R> avector <- aframe[, "a2"]
R> avector
[1]  6  7  8  9 10
R> avector <- aframe[, "a2", drop=FALSE]
R> avector
  a2
1  6
2  7
3  8
4  9
5 10
R> 

Solution 5

You can try something like this-

as.vector(unlist(aframe$a2))
Share:
479,692

Related videos on Youtube

Dolan Antenucci
Author by

Dolan Antenucci

Updated on May 10, 2022

Comments

  • Dolan Antenucci
    Dolan Antenucci about 2 years

    I have a dataframe such as:

    a1 = c(1, 2, 3, 4, 5)
    a2 = c(6, 7, 8, 9, 10)
    a3 = c(11, 12, 13, 14, 15)
    aframe = data.frame(a1, a2, a3)
    

    I tried the following to convert one of the columns to a vector, but it doesn't work:

    avector <- as.vector(aframe['a2'])
    class(avector) 
    [1] "data.frame"
    

    This is the only solution I could come up with, but I'm assuming there has to be a better way to do this:

    class(aframe['a2']) 
    [1] "data.frame"
    avector = c()
    for(atmp in aframe['a2']) { avector <- atmp }
    class(avector)
    [1] "numeric"
    

    Note: My vocabulary above may be off, so please correct me if so. I'm still learning the world of R. Additionally, any explanation of what's going on here is appreciated (i.e. relating to Python or some other language would help!)

    • joran
      joran almost 13 years
      As you're seeing in the answers, a close reading of ?'[.data.frame' will take you very far.
  • Iterator
    Iterator almost 13 years
    +1 This is useful. I had gotten used to using aframe[,"a2"] because of the ability to use this with both data frames and matrices & seem to get the same results - a vector.
  • Iterator
    Iterator almost 13 years
    +1: The reminder of drop=FALSE is useful - this helps me in cases where I may select N columns from a data.frame, in those cases where N=1.
  • Roman Luštrik
    Roman Luštrik almost 13 years
    I use this when I can't foresee the number of columns selected and in case one column comes up, the result still gets passed as a data.frame with n columns. A vector may throw a monkey wrench into the functions down the line.
  • hadley
    hadley almost 13 years
    [..., drop = F] will always return a data frame
  • jimjamslam
    jimjamslam about 8 years
    This is particularly good to know because the df$x syntax returns a vector. I used this syntax for a long time, but when I had to start using df['name'] or df[n] to retrieve columns, I hit problems when I tried to send them to functions that expected vectors. Using df[[n]] or df[['x']] cleared things right up.
  • bli
    bli almost 8 years
    Why does as.vector seem to silently have no effect? Shouldn't this either return a vector or conspicuously fail?
  • Matt
    Matt almost 6 years
    aframe[['a2']] is very useful with sf objects because aframe[,"a2"] will return two columns because the geometry column is included.
  • p-robot
    p-robot over 5 years
    This is good if you want to compare two columns using identical.
  • amc
    amc almost 4 years
    Simple! Thank you!