Home » Rows & Columns

Rows & Columns

Rows and columns form the basic structure of structured data. Rows are horizontally arranged cells, running from left to right, and are referred to as records or entities in SQL. Columns, on the other hand, are vertically arranged cells, running from top to bottom, and are known as fields in SQL.

In a data frame, each column represents a variable, while each row represents an observation. Data is related both across rows and columns. This structure is used in tables, data frames, matrices, and charts to distinguish between different categories.

Data consists of facts, figures, and other information that is collected and used for analysis. It can be qualitative or quantitative, aiding in decision-making, generating insights, and creating predictive models.

         Column1        Column2         Column3

Row1     Cell1-1        Cell1-2         Cell1-3
Row2     Cell2-1        Cell2-2         Cell2-3
Row3     Cell3-1        Cell3-2         Cell3-3

In this table:

  • The rows (Row1, Row2, Row3) go horizontally from left to right.
  • The columns (Column1, Column2, Column3) go vertically from top to bottom.
  • Each cell (like Cell1-1, Cell2-2) is a specific value in the data frame.

In R, a data frame is a two-dimensional, table-like structure that consists of rows and columns. You can perform various operations to access and manipulate the data. We extract informatio based on rows and columns by indexing & slicing.

Indexing is the process of accessing specific elements within a data structure using their position or labels. In the context of data frames, you can use indexing to access particular rows, columns, or individual elements.

> mtcars[2,3] # Access the element in 2nd row & 3rd column
> mtcars$mpg # Access entire column (or)
> mtcars[ ,1] # Access entire column
> mtcars[1, ] # Access entire row

Slicing refers to extracting a subset of elements from a data structure, often based on a range or condition. This can involve selecting multiple rows or columns at once. For example,

> mtcars[1:3, 2:4] # Access rows 1 to 3 and columns 2 to 4
> mtcars[mtcars$mpg> 20, ] # Access rows where ‘ColumnName’ > 20