- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert data frame values to a vector by rows in R?
Data can be supplied to us in any form but it is possible that it is not the appropriate one that should be used for analysis. Sometimes data is recorded in a data frame but we might need it as a vector. In such type of situation, we have to change the values of our data frame in a vector. This can be done by reading the data frame values by reading them as.vector after transposing the data frame with t.
Example
Consider the below data frame −
> x1<-rep(c(1,2,3,4,5),times=4) > x2<-1:20 > x3<-rep(c(5,10,15,20),each=5) > df<-data.frame(x1,x2,x3) > df x1 x2 x3 1 1 1 5 2 2 2 5 3 3 3 5 4 4 4 5 5 5 5 5 6 1 6 10 7 2 7 10 8 3 8 10 9 4 9 10 10 5 10 10 11 1 11 15 12 2 12 15 13 3 13 15 14 4 14 15 15 5 15 15 16 1 16 20 17 2 17 20 18 3 18 20 19 4 19 20 20 5 20 20 > as.vector(t(df)) [1] 1 1 5 2 2 5 3 3 5 4 4 5 5 5 5 1 6 10 2 7 10 3 8 10 4 9 10 5 10 10 1 11 15 2 12 15 3 13 15 4 14 15 5 15 15 1 16 20 2 17 20 3 18 20 [55] 4 19 20 5 20 20 > Vector_df<-as.vector(t(df)) > sum(Vector_df) [1] 520 > mean(Vector_df) [1] 8.666667
Let’s have a look at another example −
> y1<-20:1 > y2<-rep(c(2,4,6,8,10),times=4) > y3<-c(22,25,27,21,18,19,28,24,29,15,27,24,16,18,28,17,18,26,23,22) > df_y<-data.frame(y1,y2,y3) > df_y y1 y2 y3 1 20 2 22 2 19 4 25 3 18 6 27 4 17 8 21 5 16 10 18 6 15 2 19 7 14 4 28 8 13 6 24 9 12 8 29 10 11 10 15 11 10 2 27 12 9 4 24 13 8 6 16 14 7 8 18 15 6 10 28 16 5 2 17 17 4 4 18 18 3 6 26 19 2 8 23 20 1 10 22 > Vector_new<-as.vector(t(df_y)) > Vector_new [1] 20 2 22 19 4 25 18 6 27 17 8 21 16 10 18 15 2 19 14 4 28 13 6 24 12 8 29 11 10 15 10 2 27 9 4 24 8 6 16 7 8 18 6 10 28 5 2 17 4 4 18 3 6 26 [55] 2 8 23 1 10 22
- Related Articles
- How to create a vector of data frame values by rows in R?
- How to convert a vector into data frame in R?
- How to convert values in alternate rows to negative in R data frame column?
- How to convert a vector to data frame in R by defining number of columns?
- How to convert a data frame row into character vector in R?
- How to convert rows in an R data frame to list?
- How to create a character vector from data frame values in R?
- How to subset a data frame based on a vector values in R?
- Extract a data frame column values as a vector by matching a vector in R.
- How to convert data frame values to their rank in R?
- How to convert two columns of an R data frame to a named vector?
- How to convert negative values in an R data frame to positive values?
- How to convert columns of an R data frame into a single vector?
- How to convert columns of an R data frame into rows?
- How to merge rows having same values in an R data frame?

Advertisements