Convert data.frame column to vector?

I have a dataframe, for example:

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 does not work:

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

This is the only solution I could come up with, but I assume that there should 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 dictionary above may be turned off, so please correct me if so. I am still exploring the world of R. Also, any explanation of what is going on here is appreciated (for example, regarding Python or some other language, it would help!)

+136
type-conversion vector r dataframe
Aug 15 '11 at 20:08
source share
9 answers

I will try to explain this without making any mistakes, but I am sure that it will attract clarifications or two in the comments.

A data frame is a list. When you multiply a data frame using the column name and [ , what you get is a sublist (or frame of subordinate data). If you need the actual atomic column, you can use [[ or somewhat vaguely (for me), you could make aframe[,2] , which returns a vector, not a sublist.

So, try running this sequence, and perhaps everything will be clearer:

 avector <- as.vector(aframe['a2']) class(avector) avector <- aframe[['a2']] class(avector) avector <- aframe[,2] class(avector) 
+184
Aug 15 '11 at 20:19
source share

You can use extracting $ :

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

or double square brackets:

 class(aframe[["a1"]]) [1] "numeric" 
+30
Aug 15 '11 at 20:20
source share

Now there is an easy way to do this with dplyr .

 dplyr::pull(aframe, a2) 
+24
Jan 08 '18 at 17:21
source share

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

Another thing to be aware of is the drop=FALSE option for [ :

 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> 
+19
Aug 15 '11 at 20:19
source share

Another advantage of using the [['operator is that it works with both data.frame and data.table. Therefore, if a function should be run for both data.frame and data.table, and you want to extract a column from it as a vector, then

 data[["column_name"]] 

better.

+9
Sep 14 '16 at 7:26
source share

You can try something like this-

 as.vector(unlist(aframe$a2)) 
+5
Oct 05 '18 at 3:48
source share

If you just use the extract statement, it will work. By default, [] sets the drop=TRUE option that you want here. See ?'[' For more details.

 > 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) > aframe[,'a2'] [1] 6 7 8 9 10 > class(aframe[,'a2']) [1] "numeric" 
+4
Aug 15 '11 at 20:20
source share
 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) avector <- as.vector(aframe['a2']) avector<-unlist(avector) #this will return a vector of type "integer" 
+2
Jul 02 '17 at 13:52
source share

I use lists to filter information frames by whether they have the value% in% of the list.

I manually created lists by exporting a 1-column data frame in Excel, where I would add a "" around each item before inserting it into R: List <- c ("el1", "el2", ...), which is usually was then followed by FilteredData <- subset (Data, Column% in list%).

After searching on stackoverflow and not finding an intuitive way to convert a 1-column data frame to a list, I am now publishing my first contribution to stackoverflow:

 # assuming you have a 1 column dataframe called "df" list <- c() for(i in 1:nrow(df)){ list <- append(list, df[i,1]) } View(list) # This list is not a dataframe, it is a list of values # You can filter a dataframe using "subset([Data], [Column] %in% list") 
+1
Dec 03 '18 at 18:17
source share



All Articles