Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Logical Indexing | Data Frames
R Introduction: Part II
course content

Contenido del Curso

R Introduction: Part II

R Introduction: Part II

1. Matrices
2. Data Frames
3. Lists

bookLogical Indexing

Good! Accessing columns by their names is convenient. Can we filter the rows we want to output?

Indeed, we can. First, we can use indices (like it was for vectors or matrices). But usually, we do not know the positions of the rows but know some conditions we want to satisfy. For example, we may want to extract data for only Males or only people older than 30. You can do it by specifying necessary conditions within square brackets. You need to use the double sign == for equality.

Assume we have data frame data and want to filter to rows having the value 30 in column age. This can be done using the following syntax: data[data$age == 30,]. Note that you put condition as the first index within the square bracket. For example, for the same training data as before, let's extract the data of people older than 30 and males only.

1234567891011
# Data name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") # Creating a data frame test <- data.frame(name, age, gender) # People older than 30 test[test$age > 30, ] # Males only test[test$gender == 'M', ]
copy

As you can see, that's correct.

Tarea

Using the mtcars dataset, extract the following data:

  1. The cars pass a quarter-mile in less than 16 seconds (qsec column).
  2. Cars with 6 cylinders (cyl column).

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 4
toggle bottom row

bookLogical Indexing

Good! Accessing columns by their names is convenient. Can we filter the rows we want to output?

Indeed, we can. First, we can use indices (like it was for vectors or matrices). But usually, we do not know the positions of the rows but know some conditions we want to satisfy. For example, we may want to extract data for only Males or only people older than 30. You can do it by specifying necessary conditions within square brackets. You need to use the double sign == for equality.

Assume we have data frame data and want to filter to rows having the value 30 in column age. This can be done using the following syntax: data[data$age == 30,]. Note that you put condition as the first index within the square bracket. For example, for the same training data as before, let's extract the data of people older than 30 and males only.

1234567891011
# Data name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") # Creating a data frame test <- data.frame(name, age, gender) # People older than 30 test[test$age > 30, ] # Males only test[test$gender == 'M', ]
copy

As you can see, that's correct.

Tarea

Using the mtcars dataset, extract the following data:

  1. The cars pass a quarter-mile in less than 16 seconds (qsec column).
  2. Cars with 6 cylinders (cyl column).

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 4
toggle bottom row

bookLogical Indexing

Good! Accessing columns by their names is convenient. Can we filter the rows we want to output?

Indeed, we can. First, we can use indices (like it was for vectors or matrices). But usually, we do not know the positions of the rows but know some conditions we want to satisfy. For example, we may want to extract data for only Males or only people older than 30. You can do it by specifying necessary conditions within square brackets. You need to use the double sign == for equality.

Assume we have data frame data and want to filter to rows having the value 30 in column age. This can be done using the following syntax: data[data$age == 30,]. Note that you put condition as the first index within the square bracket. For example, for the same training data as before, let's extract the data of people older than 30 and males only.

1234567891011
# Data name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") # Creating a data frame test <- data.frame(name, age, gender) # People older than 30 test[test$age > 30, ] # Males only test[test$gender == 'M', ]
copy

As you can see, that's correct.

Tarea

Using the mtcars dataset, extract the following data:

  1. The cars pass a quarter-mile in less than 16 seconds (qsec column).
  2. Cars with 6 cylinders (cyl column).

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Good! Accessing columns by their names is convenient. Can we filter the rows we want to output?

Indeed, we can. First, we can use indices (like it was for vectors or matrices). But usually, we do not know the positions of the rows but know some conditions we want to satisfy. For example, we may want to extract data for only Males or only people older than 30. You can do it by specifying necessary conditions within square brackets. You need to use the double sign == for equality.

Assume we have data frame data and want to filter to rows having the value 30 in column age. This can be done using the following syntax: data[data$age == 30,]. Note that you put condition as the first index within the square bracket. For example, for the same training data as before, let's extract the data of people older than 30 and males only.

1234567891011
# Data name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") # Creating a data frame test <- data.frame(name, age, gender) # People older than 30 test[test$age > 30, ] # Males only test[test$gender == 'M', ]
copy

As you can see, that's correct.

Tarea

Using the mtcars dataset, extract the following data:

  1. The cars pass a quarter-mile in less than 16 seconds (qsec column).
  2. Cars with 6 cylinders (cyl column).

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 2. Capítulo 4
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt