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”.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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 –
- How to Create a DataFrame in R?
- How to Add a Row to a Dataframe in R?
- R – Get Vector of Dataframe Column Names
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.