Exercise

Extract data frame cell value

While working with tables you need to be able to select a specific data value from any row or column.

Recall that in Microsoft Excel, you can select a cell by specifying its location in the spreadsheet. For example, cell A1 represents column A and row 1. In data frames in R, the location of a cell is specified by row and column numbers.

Check out the different syntaxes which can be used for extracting data:

  • Extract value of a single cell: df_name[x, y], where x is the row number and y is the column number of a data frame called df_name.
  • Extract the entire row: df_name[x, ], where x is the row number. By not specifying the column number, we automatically choose all the columns for row x.
  • Extract the entire column: df_name[, y] where y is the column number. By not specifying the row number, we automatically choose all the rows for column y.

Another way to extract data from df_name is by using the dollar sign in combination with the column names:

  • df_name$colname refers to the entire column col_name in df_name data frame.
  • df_name$colname[x] refers to row x of column colname in data frame df_name.

Which of the following lines will give you the correct value of the closing price in the 100th row of data_maruti? The data_maruti data frame is already available in the workspace. The name of the column containing closing prices is Close.

Instructions

50 XP

Possible answers