The Wayback Machine - https://web.archive.org/web/20240824013619/https://www.geeksforgeeks.org/convert-an-object-to-data-frame-in-r-programming-as-data-frame-function/
Open In App

Convert an Object to Data Frame in R Programming – as.data.frame() Function

Last Updated : 22 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

as.data.frame() function in R Programming Language is used to convert an object to data frame. These objects can be Vectors, Lists, Matrices, and Factors.

Syntax: as.data.frame(object)

Parameters: 

  • object: Vector, Matrix, factor, or data frame

R – as.data.frame() Function Example

Example 1: Basic example of as.data.frame() Function in R

R




# R program to convert object to data frame
 
# Creating Vectors
x1 <- c(1, 2, 3, 4)
x2 <- c("a", "B", "C", "D")
x3 <- c("hello", "Geeks", "for", "geeks")
 
# Creating list of vectors
x <- list(col1 = x1, col2 = x2, col2 = x3)
x
 
# Converting list to data frame
as.data.frame(x)


Output: 

$col1
[1] 1 2 3 4

$col2
[1] "a" "B" "C" "D"

$col2
[1] "hello" "Geeks" "for"   "geeks"

  col1 col2 col2.1
1    1    a  hello
2    2    B  Geeks
3    3    C    for
4    4    D  geeks

Example 2: 

R




# R program to convert object to list
 
# Converting pre-defined dataset to list
x < - as.list(BOD)
x
 
# Converting list to data frame
as.data.frame(x)


Output: 

$Time
[1] 1 2 3 4 5 7

$demand
[1]  8.3 10.3 19.0 16.0 15.6 19.8

attr(, "reference")
[1] "A1.4, p. 270"
  Time demand
1    1    8.3
2    2   10.3
3    3   19.0
4    4   16.0
5    5   15.6
6    7   19.8


Similar Reads

Check if the Object is a Data Frame in R Programming - is.data.frame() Function
is.data.frame() function in R Language is used to return TRUE if the specified data type is a data frame else return FALSE. R data.frame is a powerful data type, especially when processing table (.csv). It can store the data as row and columns according to the table. Syntax: is.data.frame(x) Parameters: x: specified data.frame Example 1: # R progra
1 min read
Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function
data.matrix() function in R Language is used to create a matrix by converting all the values of a Data Frame into numeric mode and then binding them as a matrix. Syntax: data.matrix(df) Parameters: df: Data frame to be converted. Example 1: # R program to convert a data frame # into a numeric matrix # Creating a dataframe df1 = data.frame( &quot;Na
2 min read
Difference Between as.data.frame() and data.frame() in R
R Programming Language contains a large number of data structures, data frames being very crucial in it. It is used to organize the data in a well-arranged tabular manner. Data frames can both be created from scratch as well as other data objects can be converted to data frames easily using large inbuilt R methods. as.data.frame() The as.data.frame
2 min read
Convert a Data Frame into a Molten Form in R Programming - melt() Function
function in R Language is used to combine multiple columns of s Data Frame into a single column. Syntax: melt(x, na.rm, value.name) Parameters: x: data to be melted na.rm: Boolean value to remove NA value.name: Setting column names Example 1: # R program to reshape data frame # Loading library library(reshape) # Creating a data frame a &lt;- data.f
2 min read
Convert type of data object in R Programming - type.convert() Function
type.convert() function in R Language is used to compute the data type of a particular data object. It can convert data object to logical, integer, numeric, or factor. Syntax: type.convert(x) Parameter: x: It can be a vector matrix or an array Example 1: Apply type.convert to vector of numbers # R program to illustrate # type.convert to vector of n
2 min read
Modify Data of a Data Frame with an Expression in R Programming - with() Function
with() function in R Language is used to modify data of a data frame by evaluating an expression within the arguments of the function. Syntax: with(x, expr) Parameters: x: Data frame expr: Expression to modify data Example 1: # R program to modify data of an object # Calling predefined data set BOD # Calling with() function with(BOD, {BOD$demand
1 min read
Get Transpose of a Matrix or Data Frame in R Programming - t() Function
t() function in R Language is used to calculate transpose of a matrix or Data Frame. Syntax: t(x) Parameters: x: matrix or data frame Example 1: # R program to illustrate # t function # Getting R Biochemical Oxygen Demand Dataset BOD # Calling t() function t(BOD) Output: Time demand 1 1 8.3 2 2 10.3 3 3 19.0 4 4 16.0 5 5 15.6 6 7 19.8 [, 1] [, 2] [
1 min read
Choose Specific Columns of a Data Frame in R Programming - select() Function
select() function in R Language is used to choose whether a column of the data frame is selected or not. Syntax: select(x, expr) Parameters: x: Data frame expr: condition for selection Example 1: # R program to select specific columns # Loading library library(dplyr) # Create a data frame d &lt;- data.frame( name = c(&quot;Abhi&quot;, &quot;Bhavesh
2 min read
Condense Column Values of a Data Frame in R Programming - summarise() Function
summarise() function in R Language is used to condense various values of column of a data frame to one value. Syntax: summarise(x, expr) Parameters: x: Data Frame expr: Operation to condense data Example 1: # R program to condense data # of a data frame # Loading library library(dplyr) # Create a data frame d &lt;- data.frame( name = c(&quot;Abhi
1 min read
Take Random Samples from a Data Frame in R Programming - sample_n() Function
sample_n() function in R Language is used to take random sample specimens from a data frame. Syntax: sample_n(x, n) Parameters: x: Data Frame n: size/number of items to select Example 1: # R program to collect sample data # from a data frame # Loading library library(dplyr) # Create a data frame d &lt;- data.frame( name = c(&quot;Abhi&quot;, &quot;
1 min read
Create Subsets of a Data frame in R Programming - subset() Function
subset() function in R Programming Language is used to create subsets of a Data frame. This can also be used to drop columns from a data frame. Syntax: subset(df, expr) Parameters: df: Data frame usedexpr: Condition for subsetCreate Subsets of Data Frames in R Programming LanguageHere we will make subsets of dataframe using subset() methods in R la
3 min read
Rename Columns of a Data Frame in R Programming - rename() Function
In this article, we will discuss how to Rename the Columns of a Data Frame in R Programming Language using the rename() Function. rename function in Rrename() function in R Programming Language is used to rename the column names of a data frame, based on the older names. Syntax: rename(x, names) Parameters: x: Data frame names: Old name and new nam
2 min read
Accessing variables of a data frame in R Programming - attach() and detach() function
In this article, we will see how to Access variables of a data frame in R Programming Language. R - Accessing variables of a data frameMethod 1: Using attach() function in Rattach() function in R Language is used to access the variables present in the data framework without calling the data frame. Syntax: attach(data, pos) Parameters: data: data fr
2 min read
data.table vs data.frame in R Programming
data.table in R is an enhanced version of the data.frame. Due to its speed of execution and the less code to type it became popular in R. The purpose of data.table is to create tabular data same as a data frame but the syntax varies. In the below example let we can see the syntax for the data table: C/C++ Code DataTable = data.table(name = c(&quot;
3 min read
Convert an Object to List in R Programming - as.list() Function
as.list() function in R Programming Language is used to convert an object to a list. These objects can be Vectors, Matrices, Factors, and dataframes. Syntax: as.list(object) Parameters: object: Vector, Matrix, factor, or data frame R - as.list() Function ExampleExample 1: Converting Vector to list using as.list() function in R Language C/C++ Code #
2 min read
Convert a Numeric Object to Character in R Programming - as.character() Function
as.character() function in R Language is used to convert a numeric object to character object. Syntax: as.character(x) Parameters: x: Numeric Object Example 1: # R program to convert a numeric object # to character object # Calling as.character() function as.character(1) as.character(2 + 3) as.character(1.5) Output: [1] "1" [1] "5" [1] "1.5" Exampl
1 min read
Convert a Character Object to Integer in R Programming - as.integer() Function
as.integer() function in R Language is used to convert a character object to integer object. Syntax: as.integer(x) Parameters: x: Character Object Example 1: # R program to convert a character object # to an integer object # Calling as.integer() function as.integer(&quot;4&quot;) as.integer(&quot;1.6&quot;) as.integer(&quot;-3.2&quot;) as.integer(
1 min read
Convert values of an Object to Logical Vector in R Programming - as.logical() Function
as.logical() function in R Language is used to convert an object to a logical vector. Syntax: as.logical(x)Parameters: x: Numeric or character object R - as.logical() Function ExampleExample 1: Basic example of as.logical() Function in R Programming Language. C/C++ Code # R Program to convert # an object to logical vector # Creating a vector x &lt;
1 min read
Convert an Object into a Vector in R Programming - as.vector() Function
as.vector() function in R Language is used to convert an object into a vector. Syntax: as.vector(x) Parameters: x: Object to be converted Example 1: # R program to convert an object to vector # Creating an array x &lt;- array(c(2, 3, 4, 7, 2, 5), c(3, 2)) x # Calling as.vector() Function as.vector(x) Output: [, 1] [, 2] [1, ] 2 7 [2, ] 3 2 [3, ] 4
1 min read
Convert an Object to a String in R Programming - toString() Function
toString() function in R Language is used to convert an object into a single character string. Syntax: toString(x, width) Parameters: x: Object width: maximum string width Example 1: # R program to convert an object to string # Creating a vector x &lt;- c(&quot;Geeks&quot;, &quot;for&quot;, &quot;geeks&quot;) # Calling toString() Function toString(
1 min read
Convert an Object to a Table in R Programming - as.table() Function
as.table() function in R Language is used to convert an object into a table. Syntax: as.table(x) Parameters: x: Object to be converted Example 1: # R Program to convert # an object to a table # Creating a vector vec = c(2, 4, 3, 1, 2, 3, 2, 1, 4, 2) # Calling as.table() Function as.table(vec) Output: A B C D E F G H I J 2 4 3 1 2 3 2 1 4 2 Example
1 min read
Convert an Object into a Matrix in R Programming - as.matrix() Function
In R Programming Language as. matrix() function is used to convert an object into a Matrix. Syntax: as.matrix(x) Parameters: x: Object to be converted as.matrix() Function in R ExampleExample 1: Convert vector to the matrix using as.matrix() C/C++ Code # R program to convert an object to matrix # Creating a vector x &lt;- c(1:9) # Calling as.matrix
2 min read
Create Matrix and Data Frame from Lists in R Programming
In R programming, there 5 basic objects. Lists are the objects that can contain heterogeneous types of elements, unlike vectors. Matrices can contain the same type of elements or homogeneous elements. On the other hand, data frames are similar to matrices but have an advantage over matrices to keep heterogeneous elements. In this article, we'll lea
3 min read
Creating a Data Frame from Vectors in R Programming
A vector can be defined as the sequence of data with the same datatype. In R, a vector can be created using c() function. R vectors are used to hold multiple data values of the same datatype and are similar to arrays in C language. Data frame is a 2 dimensional table structure which is used to hold the values. In the data frame, each column contain
5 min read
Convert Data Frame Column to Numeric in R
R DataFrame is made up of three principal components, the data, rows, and columns. Data frames in R are versatile data structures to matrices where each column can contain different data types. This versatility allows for complex data analysis tasks. Converting columns to numeric type is a common operation in data analysis workflows, enabling mathe
4 min read
Convert Tibble to Data Frame in R
Tibbles are a type of data frame in R Programming Language that has an enhanced print method, making it easier to display data. However, in some situations, we may need to convert a tibble to a data frame. So, in this article, we will explore some approaches to convert tibbles to data frames. Tibble: tibble is a modern version of a data frame in R,
4 min read
How to Convert Latitude and Longitude String Vector into Data Frame in R?
In geographic data analysis and visualization, latitude and longitude coordinates are fundamental for mapping and spatial analysis. In R, converting latitude and longitude string vectors into a data frame allows you to manipulate, analyze, and visualize spatial data effectively. This article provides a comprehensive guide on how to achieve this pro
2 min read
Applying a Function over an Object in R Programming - sapply() Function
sapply() function in R Language takes list, vector or data frame as input and gives output in vector or matrix. It is useful for operations on list objects and returns a list object of same length of original set. Syntax: sapply(X, FUN) Parameters: X: A vector or an object FUN: Function applied to each element of x Example 1: # R program to illustr
1 min read
Check if Object is of the Character Data type in R Programming - is.character() Function
is.character() Function in R Language is used to check if the object is of the form of a string/character or not. It will return true if any element of the object is of the character data type. Syntax: is.character(Object) Parameter: Object: It could be an array, list, vector, etc. Example 1: # R Program to illustrate # the use of is.character func
1 min read
Check if an Object is of Complex Data Type in R Programming - is.complex() Function
is.complex() function in R Language is used to check if the object passed to it as argument is of complex data type. Syntax: is.complex(x) Parameters: x: Object to be checked Example 1: # R program to check if # object is of complex type # Calling is.complex() function is.complex(1 + 0i) is.complex(1.5 + 2i) is.complex(-1.5 + 2i) Output: [1] TRUE [
1 min read