count number of columns in R dataframe

Get Number of Columns in R Dataframe

A dataframe in R is a data structure used to store data in a tabular form – in rows and columns. In this tutorial, we will look at how to get the number of columns in an R dataframe with the help of some examples.

How do I count the columns in a dataframe in R?

You can use the built-in ncol() function to count the number of columns in a dataframe in R. Pass the dataframe as an argument.

The following is the syntax –

# number of columns in dataframe
ncol(dataframe)

It returns the number of columns in the dataframe.

Examples

Let’s now look at some examples of using the above syntax to count columns in a dataset in R.

Example 1 – Count columns in an R dataframe

First, let’s create a dataframe that we will be operating on.

# create a dataframe
employees_df = data.frame(
  "Name"= c("Jim", "Dwight", "Angela", "Tobi", "Kevin"),
  "Age"= c(26, 28, 29, 32, 30),
  "Department"= c("Sales", "Sales", "Accounting", "HR", "Accounting"),
  "Salary" = c(80000, 81000, 72000, 65000, 72000)
)
# display the dataframe
print(employees_df)

Output:

    Name Age Department Salary
1    Jim  26      Sales  80000
2 Dwight  28      Sales  81000
3 Angela  29 Accounting  72000
4   Tobi  32         HR  65000
5  Kevin  30 Accounting  72000

We now have a dataframe containing information about some employees working in an office. The dataframe has columns “Name”, “Age”, “Department”, and “Salary”.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

We can see that the above dataframe has four columns. Let’s now try to get this column count programmatically in R.

To get the column count of the above dataframe, we pass the dataframe as an argument to the ncol() function.

# number of columns in employees_df
print(ncol(employees_df))

Output:

[1] 4

We get the number of columns in the above dataframe as 4.

Example 2 – Number of columns in an R dataframe with NA values

What happens if the dataframe you want to count the columns for has NA values?

Let’s find out.

First, we will create a dataframe with some NA values.

# create a dataframe
employees_df = data.frame(
  "Name"= c("Jim", "Dwight", "Angela", "Tobi", "Kevin"),
  "Age"= c(26, 28, NA, 32, 30),
  "Department"= c("Sales", "Sales", "Accounting", "HR", "Accounting"),
  "Salary" = c(80000, 81000, 72000, NA, 72000)
)
# display the dataframe
print(employees_df)

Output:

    Name Age Department Salary
1    Jim  26      Sales  80000
2 Dwight  28      Sales  81000
3 Angela  NA Accounting  72000
4   Tobi  32         HR     NA
5  Kevin  30 Accounting  72000

Here, we created a similar dataframe from the above example by replacing some values with NA. You can see that columns “Age” and “Salary” have one NA value each.

Let’s now apply the ncol() function to this dataframe.

# number of columns in employees_df
print(ncol(employees_df))

Output:

[1] 4

We get the number of columns in the above dataframe as 4. Same as the result we got when the dataframe didn’t have any NA values. We can say that the ncol() function counts the columns in the dataframe irrespective of the NA values present.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Authors

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

  • Gottumukkala Sravan Kumar
Scroll to Top