The “ncol” Function in R

  • Package: Base R (no specific package required)

  • Purpose: Retrieves the number of columns in an array, matrix, or data frame.

  • General Class: Data Manipulation

  • Required Argument(s):

    • An array, matrix, or data frame for which the number of columns is to be retrieved.

  • Notable Optional Arguments:

    • None

  • Example:

  • # Example data for using the ncol function
    matrix_example <- matrix(1:6, nrow = 2, ncol = 3)

    # Get the number of columns in the matrix
    num_cols <- ncol(matrix_example)

    # Display the result
    print(num_cols)

  • In this example, the ncol function is used to retrieve the number of columns in a matrix (matrix_example). The resulting num_cols value is 3, as specified when creating the matrix. The ncol function is commonly used to determine the number of columns in matrices and data frames.

Previous
Previous

The “sapply” Function in R

Next
Next

The “nrow” Function in R