To find the size of data frame in R, you can use different function depending on you requirement.

The following methods show how you can do it with syntax.

Method 1: Use nrow() Function

nrow(df)


Method 2: Use ncol() Function

ncol(df)


Method 3: Use dim() Function

dim(df)


Method 4: Use object.size() Function

object.size(df)


The following examples show how to find size of data frame using different function in R.

Using nrow() Function

Let’s see how we can use nrow() function to find number of rows in data frame:

# Create dataframe
df <- data.frame(Machine_name=c("A","B","C","D","A","A","G","B"),
Temperature=c(12,9,NA,13,18,28,22,23),
Pressure=c(20,25,27,29,30,32,39,40))

# Find total number of rows
nrow(df)


Output:

[1] 8


Here the output shows number of rows in dataframe using nrow() function

Using ncol() Function

Let’s see how we can use ncol() function to find number of column in data frame:

# Create dataframe
df <- data.frame(Machine_name=c("A","B","C","D","A","A","G","B"),
Temperature=c(12,9,NA,13,18,28,22,23),
Pressure=c(20,25,27,29,30,32,39,40))

# Find total number of columns
ncol(df)


Output:

[1] 3


Here the output shows number of column of dataframe using ncol() function.

Using dim() Function

Let’s see how we can use dim() function to find number of rows and column of data frame:

# Create dataframe
df <- data.frame(Machine_name=c("A","B","C","D","A","A","G","B"),
Temperature=c(12,9,NA,13,18,28,22,23),
Pressure=c(20,25,27,29,30,32,39,40))

# Find total number of rows and column
dim(df)


Output:

[1] 8 3


Here the output shows number of rows and column of dataframe using dim() function

Using object.size() Function

Let’s see how we can use object.size() Function in R to find memory size required by dataframe:

# Create dataframe
df <- data.frame(Machine_name=c("A","B","C","D","A","A","G","B"),
Temperature=c(12,9,NA,13,18,28,22,23),
Pressure=c(20,25,27,29,30,32,39,40))

object.size(df)


Output:

1480 bytes


Here the output shows memory size of dataframe.