R
Data filtering - data frames
Indexing data frames works in all respects identically to vectors with one exception: there are two dimensions you need to specify to retrieve a specific element: x (rows) and y (columns). Thus, if we take the following data frame called data:

and want to retrieve the cell found in row 4 of column 3, data[4,3] should be typed. In order to retrieve the full row/column, its index can be left empty: data[,3] will retrieve column 3, data[3,] will retrieve row 3. The comma separating the two indexes must be present in order to retrieve a full row. Otherwise, if provided only with one index, R will default to columns.
If your columns have names, you can index them by the name using the $ operator. Thus, in the data frame used above, data$age is equal to data[,2].
You can also use data frame indexing to add columns/rows to your data frame. By typing in a non-existent column's index/name (or the index of a non-existent row) and assigning it a value (or several values in a vector), this column will be created.data[5,] <- c("Anna", 54, 110)
Note that if the data type of the column is factor, your new row cannot contain values not present in the column yet. To add new values, you would need to convert the column to a string, add the row and then convert the column back to factor.
Filtering is again very similar to vectors, with one difference. You can use the masks acquired in one column/row to retrieve values from a different column/row. To do so, you first need to specify the mask and then the column/row to apply the mask to. For example:agemask <- data$age < 18data[agemask,]$IQ<- "NA"
will replace the values of the column IQ with "NA" if the participant is younger than 18. For that purpose, we first create a mask according to whether a participant's age is less than 18. Then, this mask is used to select those rows of data where this condition is fulfilled. Lastly, we specify that we are only interested in the values found in column 3. With unnamed columns, this could be achieved by typing in data[agemask][,3]. Alternatively, you can use the mask without saving it by typing the logical operation in-line with the value assignment, like thisdata[data$age<18,]$IQ<- "NA"
EXERCISE:
Recreate the data frame used here. Correct the typo in Lucy's IQ by replacing it with 95.