- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 a vector to data frame in R by defining number of columns?
If we have a vector where alternate values may create a tabular form then we might want to convert the vector into a data frame. For this purpose, we first need to convert the vector into a matrix with appropriate number of columns/rows and then read it as a data frame using as.data.frame function. Check out the below examples to understand how it works.
Example1
> x1<-c(rep(c(1,"male"),times=10),rep(c(2,"female"),times=10)) > x1
Output
[1] "1" "male" "1" "male" "1" "male" "1" "male" [9] "1" "male" "1" "male" "1" "male" "1" "male" [17] "1" "male" "1" "male" "2" "female" "2" "female" [25] "2" "female" "2" "female" "2" "female" "2" "female" [33] "2" "female" "2" "female" "2" "female" "2" "female"
Converting x1 into data frame −
> x1_df<-as.data.frame(matrix(x1,ncol=2,byrow=TRUE)) > x1_df
Output
V1 V2 1 1 male 2 1 male 3 1 male 4 1 male 5 1 male 6 1 male 7 1 male 8 1 male 9 1 male 10 1 male 11 2 female 12 2 female 13 2 female 14 2 female 15 2 female 16 2 female 17 2 female 18 2 female 19 2 female 20 2 female
Example2
Converting x2 into data frame −
> x2<-c(rep(c("Grp1",rpois(1,1)),times=10),rep(c("Grp2",rpois(1,8)),times=10)) > x2
Output
[1] "Grp1" "5" "Grp1" "5" "Grp1" "5" "Grp1" "5" "Grp1" "5" [11] "Grp1" "5" "Grp1" "5" "Grp1" "5" "Grp1" "5" "Grp1" "5" [21] "Grp2" "11" "Grp2" "11" "Grp2" "11" "Grp2" "11" "Grp2" "11" [31] "Grp2" "11" "Grp2" "11" "Grp2" "11" "Grp2" "11" "Grp2" "11"
Example2
> x2_df<-as.data.frame(matrix(x2,ncol=2,byrow=TRUE)) > x2_df
Output
V1 V2 1 Grp1 5 2 Grp1 5 3 Grp1 5 4 Grp1 5 5 Grp1 5 6 Grp1 5 7 Grp1 5 8 Grp1 5 9 Grp1 5 10 Grp1 5 11 Grp2 11 12 Grp2 11 13 Grp2 11 14 Grp2 11 15 Grp2 11 16 Grp2 11 17 Grp2 11 18 Grp2 11 19 Grp2 11 20 Grp2 11
Example3
Converting x3 into data frame −
> x3<-c(rep(c("Grp1",rnorm(1),rpois(1,10)),times=10),rep(c("Grp2",rnorm(1),rpois(1,2)),times=10)) > x3
Output
[1] "Grp1" "0.756362500132569" "4" [4] "Grp1" "0.756362500132569" "4" [7] "Grp1" "0.756362500132569" "4" [10] "Grp1" "0.756362500132569" "4" [13] "Grp1" "0.756362500132569" "4" [16] "Grp1" "0.756362500132569" "4" [19] "Grp1" "0.756362500132569" "4" [22] "Grp1" "0.756362500132569" "4" [25] "Grp1" "0.756362500132569" "4" [28] "Grp1" "0.756362500132569" "4" [31] "Grp2" "-0.339861988439845" "1" [34] "Grp2" "-0.339861988439845" "1" [37] "Grp2" "-0.339861988439845" "1" [40] "Grp2" "-0.339861988439845" "1" [43] "Grp2" "-0.339861988439845" "1" [46] "Grp2" "-0.339861988439845" "1" [49] "Grp2" "-0.339861988439845" "1" [52] "Grp2" "-0.339861988439845" "1" [55] "Grp2" "-0.339861988439845" "1" [58] "Grp2" "-0.339861988439845" "1"
Example4
> x3_df<-as.data.frame(matrix(x3,ncol=3,byrow=TRUE)) > x3_df
Output
V1 V2 V3 1 Grp1 0.756362500132569 4 2 Grp1 0.756362500132569 4 3 Grp1 0.756362500132569 4 4 Grp1 0.756362500132569 4 5 Grp1 0.756362500132569 4 6 Grp1 0.756362500132569 4 7 Grp1 0.756362500132569 4 8 Grp1 0.756362500132569 4 9 Grp1 0.756362500132569 4 10 Grp1 0.756362500132569 4 11 Grp2 -0.339861988439845 1 12 Grp2 -0.339861988439845 1 13 Grp2 -0.339861988439845 1 14 Grp2 -0.339861988439845 1 15 Grp2 -0.339861988439845 1 16 Grp2 -0.339861988439845 1 17 Grp2 -0.339861988439845 1 18 Grp2 -0.339861988439845 1 19 Grp2 -0.339861988439845 1 20 Grp2 -0.339861988439845 1
- Related Articles
- How to convert columns of an R data frame into a single vector?
- How to convert two columns of an R data frame to a named vector?
- How to convert data frame values to a vector by rows in R?
- How to convert a vector into data frame in R?
- How to divide data frame rows by number of columns in R?
- How to convert a data frame row into character vector in R?
- How to multiply vector values in sequence with columns of a data frame in R?
- How to select columns of an R data frame that are not in a vector?
- How to convert a data frame with categorical columns to numeric in R?
- How to convert columns of an R data frame into rows?
- How to create a vector of data frame values by rows in R?
- How to convert row index number or row index name of an R data frame to a vector?
- How to sort a data frame in R by multiple columns together?
- How to convert a character data frame to numeric data frame in R?
- How to convert dot to comma for numerical columns in an R data frame?

Advertisements