Using R convert data.frame to simple vector
see ?unlist
Given a list structure x, unlist simplifies it to produce a vector
which contains all the atomic components which occur in x.
unlist(v.row)
[1] 177 165 177 177 177 177 145 132 126 132 132 132 126 120 145 167 167 167
167 165 177 177 177 177
EDIT
You can do it with as.vector
also, but you need to provide the correct mode:
as.vector(v.row,mode='numeric')
[1] 177 165 177 177 177 177 145 132 126 132 132 132 126 120 145 167 167
167 167 165 177 177 177 177
Convert data.frame column to a vector?
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)
Convert data frame into vector
Basically, data frames are lists, so that you can invoke unlist(df)
.
Convert a dataframe to a vector (by rows)
You can try as.vector(t(test))
. Please note that, if you want to do it by columns you should use unlist(test)
.
Convert a row of a data frame to vector
When you extract a single row from a data frame you get a one-row data frame. Convert it to a numeric vector:
as.numeric(df[1,])
As @Roland suggests, unlist(df[1,])
will convert the one-row data frame to a numeric vector without dropping the names. Therefore unname(unlist(df[1,]))
is another, slightly more explicit way to get to the same result.
As @Josh comments below, if you have a not-completely-numeric (alphabetic, factor, mixed ...) data frame, you need as.character(df[1,])
instead.
convert a row of a data frame to a simple vector in R
Example from mtcars
data
mydata<-mtcars
k<-mydata[1,]
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4
names(k)<-NULL
unlist(c(k))
[1] 21.00 6.00 160.00 110.00 3.90 2.62 16.46 0.00 1.00 4.00 4.00
Updated as per @Ananda: unlist(mydata[1, ], use.names = FALSE)
Convert dataframe into named vectors based on column names
You could do something like this (though @MartinGal gives a better method). We can convert each column to a list, then flatten
to just have a named vector, then can save to the global environment.
library(tidyverse)
list2env(flatten(apply(df, 2, function(x) as.list(x))), envir = .GlobalEnv)
Convert entire data frame into one long column (vector)
The mysterious operation you are looking for is called unlist
:
> df <- data.frame(C1=c(1,NA,3),C2=c(4,5,NA),C3=c(NA,8,9))
> unlist(df, use.names = F)
[1] 1 NA 3 4 5 NA NA 8 9
Related Topics
Add Regression Plane to 3D Scatter Plot in Plotly
Set a Functions Environment to That of the Calling Environment (Parent.Frame) from Within Function
Using R to Download Zipped Data File, Extract, and Import .Csv
All Possible Combinations of a Set That Sum to a Target Value
Adding a Counter Column for a Set of Similar Rows in R
Automated Httr Authentication with Twitter , Provide Response to Interactive Prompt in "Batch" Mode
Make a List of Many Objects from a Vector of Object Names
Ggplot2: Flip Axes and Maintain Aspect Ratio of Data
Get Monthly Means from Dataframe of Several Years of Daily Temps
Milliseconds Puzzle When Calling Strptime in R
How to Display Emojis in Ggplot2 Using Emo Package in R
R: Calculate Cosine Distance from a Term-Document Matrix with Tm and Proxy
In R Plotly Subplot Graph, How to Show Only One Legend
Shiny - Checkbox in Table in Shiny
Dplyr Rowwise Sum and Other Functions Like Max
How to Declare a Thousand Separator in Read.Csv