- 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 into data frame in R?
If we want to convert an object into a data frame then as.data.frame function can be used, we just need to read the object that we want to convert with as.data.frame. For example, if we have a vector x then it can be converted to data frame by using as.data.frame(x) and this can be done for a matrix as well.
Example1
x<−rnorm(20,52574,39.6) x
Output
[1] 52561.63 52525.55 52515.01 52599.97 52489.79 52558.86 52582.89 52598.37 [9] 52571.41 52652.79 52560.56 52574.75 52565.19 52628.24 52605.80 52565.04 [17] 52548.05 52576.79 52586.98 52599.46
Example
df_x<−as.data.frame(x) df_x
Output
x 1 52561.63 2 52525.55 3 52515.01 4 52599.97 5 52489.79 6 52558.86 7 52582.89 8 52598.37 9 52571.41 10 52652.79 11 52560.56 12 52574.75 13 52565.19 14 52628.24 15 52605.80 16 52565.04 17 52548.05 18 52576.79 19 52586.98 20 52599.46
Example2
y<−rpois(20,5) y
Output
[1] 0 9 4 4 9 10 3 3 4 3 4 1 5 5 5 5 3 3 5 4
Example
df_y<−as.data.frame(y) df_y
Output
y 1 0 2 9 3 4 4 4 5 9 6 10 7 3 8 3 9 4 10 3 11 4 12 1 13 5 14 5 15 5 16 5 17 3 18 3 19 5 20 4
Example3
z<−rexp(20,0.35) z [1] 1.9134866 0.7739231 20.0291607 3.8254519 1.3787014 1.4096399 [7] 3.6788511 0.8191879 0.5512983 0.7590994 2.5126237 2.5716290 [13] 4.2588730 0.3305763 1.5971879 5.2049425 1.8465026 1.9311743 [19] 1.4250772 1.4123976 df_z<−as.data.frame(z) df_z
Output
z 1 1.9134866 2 0.7739231 3 20.0291607 4 3.8254519 5 1.3787014 6 1.4096399 7 3.6788511 8 0.8191879 9 0.5512983 10 0.7590994 11 2.5126237 12 2.5716290 13 4.2588730 14 0.3305763 15 1.5971879 16 5.2049425 17 1.8465026 18 1.9311743 19 1.4250772 20 1.4123976
- Related Articles
- How to convert a data frame row into character vector in R?
- How to convert columns of an R data frame into a single vector?
- How to convert data frame values to a vector by rows in R?
- How to convert two columns of an R data frame to a named vector?
- How to convert a vector to data frame in R by defining number of columns?
- How to convert a vector into matrix in R?
- How to convert a string vector into an integer vector in R?
- How to convert columns of an R data frame into rows?
- How to convert first letter into capital in R data frame column?
- How to convert a character data frame to numeric data frame in R?
- How to convert numeric levels of a factor into string in R data frame?
- How to convert a vector into a diagonal matrix in R?
- How to convert a text vector into a matrix in R?
- How to convert multiple columns into single column in an R data frame?
- How to convert a data frame into two column data frame with values and column name as variable in R?

Advertisements