r create data frame

R Create Data Frame

Posted on

How To Create Data Frame in R Programming

Data Frames in R are general-purpose data objects designed for storing tabular data. They can be viewed as matrices in which each column can have a different data type. An R DataFrame consists of three main components: the data, rows, and columns.

Syntax 1 – template to create an empty DataFrame:

data.frame()

Syntax 2 – template to get DataFrame:

column1 <- c("value_1", "value_2", "value_3")
column2 <- c("value_P", "value_Q", "value_R")
df <- data.frame(column1, column2, stringsAsFactors=FALSE)
print(df)

Syntax 3 – another template to get the same DataFrame::

df <- data.frame(column1 = c("value_1", "value_2", "value_3"),
column2 = c("value_P", "value_Q", "value_R"),

stringsAsFactors=FALSE)
print(df)

Example 1 – Create an empty DataFrame:

data.frame()
#output:
data frame with 0 columns and 0 rows

Example 2 – Create a simple dataframe with template:

Name <- c("Jim", "George", "Maya", "Ahmed", "Saleem")
Age <- c(23, 24, 32, 31, 30)
df <- data.frame(Name, Age, stringsAsFactors=FALSE)
print(df)

#output:
    Name Age
1    Jim  23
2 George  24
3   Maya  32
4  Ahmed  31
5 Saleem  30

Example 3 – Create a simple dataframe with another template :

df <- data.frame(Name = c("Jim", "George", "Maya", "Ahmed", "Saleem"),
                 Age = c(23, 24, 32, 31, 30), stringsAsFactors=FALSE)
print(df)

#output:
    Name Age
1    Jim  23
2 George  24
3   Maya  32
4  Ahmed  31
5 Saleem  30

Example 4 – Create DataFrame with column of id, decimal, and date type:

df <- data.frame(
emp_id = c (1:5),
Name = c("Jim", "George", "Maya", "Ahmed", "Saleem"),
Age = c(23, 24, 32, 31, 30),
salary = c(2000.30,3000.20,1900.70,2500.75,2700.25),
date_start = as.Date(c("2024-01-01", "2024-09-23", "2023-12-15", "2022-05-09", "2023-02-21")),
stringsAsFactors=FALSE
)
print(df)

#output:
  emp_id   Name Age  salary date_start
1      1    Jim  23 2000.30 2024-01-01
2      2 George  24 3000.20 2024-09-23
3      3   Maya  32 1900.70 2023-12-15
4      4  Ahmed  31 2500.75 2022-05-09
5      5 Saleem  30 2700.25 2023-02-21

Create DataFrame from Matrix

Example:

#create matrix
rownames = c("row1", "row2", "row3", "row4")  
colnames = c("col1", "col2", "col3")  
m1 <- matrix(c(1:12), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))  
is.data.frame(m1)
print(m1)

# convert the matrix into dataframe 
dataframe_data=as.data.frame(m1) 
# check dataframe
is.data.frame(dataframe_data)
# print dataframe data 
print(dataframe_data) 

#output:
[1] FALSE
     col1 col2 col3
row1    1    2    3
row2    4    5    6
row3    7    8    9
row4   10   11   12

[1] TRUE
     col1 col2 col3
row1    1    2    3
row2    4    5    6
row3    7    8    9
row4   10   11   12

Conclusion:

Creating Data Frame is done by calling data.frame() function, followed by arguments into it.