You’re part of a group

Switch to your group space and start collaborating with your teammates.

You’re part of a group

Switch to your group space and start collaborating with your teammates.














Sign up
Workspace

Course Notes: Introducción a R

0
Beta
Spinner
# ¡Asigna a la teoría de variables de qué trata este capítulo!
theory <- "factors"
theory
# Vector de género
sex_vector <- c("Male", "Female", "Female", "Male", "Male")

# Convertir sex_vector en un factor
factor_sex_vector <- factor(sex_vector)

# Imprime factor_sex_vector
factor_sex_vector
# Animales
animals_vector <- c("Elephant", "Giraffe", "Donkey", "Horse")
factor_animals_vector <- factor(animals_vector)
factor_animals_vector

# Temperatura
temperature_vector <- c("High", "Low", "High","Low", "Medium")
factor_temperature_vector <- factor(temperature_vector, order = TRUE, levels = c("Low", "Medium", "High"))
factor_temperature_vector
# Código para crear factor_survey_vector
survey_vector <- c("M", "F", "F", "M", "M")
factor_survey_vector <- factor(survey_vector)

# Especifica los niveles de factor_survey_vector
levels(factor_survey_vector) <- c( "Female", "Male")

factor_survey_vector 
# Crea factor_survey_vector con levels limpios
survey_vector <- c("M", "F", "F", "M", "M")
factor_survey_vector <- factor(survey_vector)
levels(factor_survey_vector) <- c("Female", "Male")
factor_survey_vector

# Genera summary para survey_vector
summary(survey_vector)

# Genera summary para factor_survey_vector
summary(factor_survey_vector)
# Crea factor_survey_vector con levels limpios
survey_vector <- c("M", "F", "F", "M", "M")
factor_survey_vector <- factor(survey_vector)
levels(factor_survey_vector) <- c("Female", "Male")

# Male
male <- factor_survey_vector[1]

# Female
female <- factor_survey_vector[2]

# Batalla de los sexos: ¿Es el hombre «más grande» que la mujer?
male > female

¡Qué interesante! De forma predeterminada, R devuelve NA cuando intentas comparar los valores de un factor, ya que la idea no tiene sentido. A continuación, aprenderás sobre los ordered factors, en los que es posible realizar comparaciones más significativas.

# Crea speed_vector
speed_vector <- c( "medium", "slow", "slow", "medium", "fast")
# Crea speed_vector
speed_vector <- c("medium", "slow", "slow", "medium", "fast")

# Convierte speed_vector en vector de factor ordenado
factor_speed_vector <- factor(speed_vector, ordered = TRUE, levels = c("slow", "medium", "fast"))

# Imprime factor_speed_vector
factor_speed_vector
summary(factor_speed_vector)
# Crea factor_speed_vector
speed_vector <- c("medium", "slow", "slow", "medium", "fast")
factor_speed_vector <- factor(speed_vector, ordered = TRUE, levels = c("slow", "medium", "fast"))

# Valor factorial para el segundo analista de datos
da2 <-factor_speed_vector [2]

# Valor factorial para el quinto analista de datos
da5 <-  factor_speed_vector[5] 

# ¿Es el analista de datos 2 más rápido el analista de datos 5?
da2 >  da5 
# Imprime el data frame incorporado en R
mtcars
# Utliza head() en mtcars
head(mtcars)
# Investiga la estructura de mtcars
str(mtcars)
# Definición de vectores
name <- c("Mercury", "Venus", "Earth",
          "Mars", "Jupiter", "Saturn",
          "Uranus", "Neptune")
type <- c("Terrestrial planet",
          "Terrestrial planet",
          "Terrestrial planet",
          "Terrestrial planet", "Gas giant",
          "Gas giant", "Gas giant", "Gas giant")
diameter <- c(0.382, 0.949, 1, 0.532,
              11.209, 9.449, 4.007, 3.883)
rotation <- c(58.64, -243.02, 1, 1.03,
              0.41, 0.43, -0.72, 0.67)
rings <- c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE)

# Crea un data frame de los vectores
planets_df <- data.frame(name,type,diameter,rotation,rings)
planets_df



  • AI Chat
  • Code