Open In App

How to get the structure of a given DataFrame in R?

In this article, we will see how to get the structure of a DataFrame in R programming.

Structure of a dataframe generally means the inner details about the dataframe.



Steps for Getting Structure of DataFrame:

Function Used:



To get the structure of a data frame we use a built-in function called str().

Syntax: str( Dataframe_name )

We have to pass the data frame which is already created.

If we don’t pass an already created data frame we won’t get an output. 

Now, let’s see through the examples of how to create a dataframe of different column sizes in R. The examples demonstrated here are performed in R studio which is the most used IDE for R programming.

Example 1: Creating a data frame with 2 columns




df1 = data.frame(id = c(1  ,2 , 3), name = c("karthik" , "nikhil" ,"sravan"))
print(df1)

Output:

 

Example 2: Creating dataframe with 3 columns

Note: Here the command “df2” and “print(df2)” have the same result, hence “df2” is used because it’s simpler than the other. 




df2 = data.frame(sid = c(1, 2, 3),
                 sname = c("karthik" , "nikhil" , "sravan"),
                 Branch = c("IT" , "CSE" , "IT"))
df2

Output:

 

Example 3: Creating dataframe with 4 columns




df3 = data.frame(eid = c(1, 2, 3) ,
                 ename = c("krishna" , "nikhil" , "manoj"),
                 salary = c(50,000 , 60,000 , 70,000),
                 Designation = c("senior manager" , "HR" , "Manager"))
df3

Output:

 

Getting the structure of a given data frame 

Example 1: Structure of df1




df1 = data.frame(id = c(1  , 2, 3),
                 name = c("karthik" , "nikhil" , "sravan"))
str(df1)

Output:

 

Example 2: Structure of df2




df2 = data.frame(sid = c(1, 2, 3),
                 sname = c("karthik" , "nikhil" , "sravan"),
                 Branch = c("IT" , "CSE" , "IT"))
str(df2)

Output:

 

Example 3: Structure of df3




df3 = data.frame(eid = c(1, 2, 3) ,
                 ename = c("krishna" , "nikhil" , "manoj"),
                 salary = c(50000 , 60000 , 70000),
                 Designation = c("senior manager" , "HR" , "Manager"))
str(df3)

Output:

 


Article Tags :