ncol()

The ncol() function returns the number of columns in a data frame.

Syntax

ncol(df_object)

The df_object parameter is the data frame passed into the ncol() function.

Example

The following example uses a CSV file named patients.csv with the following information:

First Name Last Name DOB
Kiercen Tremboly 06/24/1992
Phaedra Blackborn 08/19/1972
Toby Spencer 11/16/1953
Sandra Speller 07/05/1989

The data within the file can be read in and the ncol() function can be used to retrieve the number of columns:

# Read file
df <- read.csv("patients.csv")
# Retrieve number of columns
ncol(df)

This results in the following output:

3

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn R on Codecademy