Using factors in conditional statements in R involves comparing factor levels or converting factors to characters or numerics to perform logical comparisons. This allows for flexible and powerful data manipulation based on categorical data.
In this example,
weather
which contains the values 'Sunny'
, 'Rainy'
, 'Cloudy'
, and 'Sunny'
. This vector represents different weather conditions.factor()
function to convert the weather
vector into a factor. We assign the result to a variable named weather_factor
. The factor()
function identifies the unique levels of the vector and converts it into a factor with those levels.weather_factor
are equal to 'Sunny'. This is done using the ==
operator.sunny_days
. This logical vector indicates which elements in weather_factor
are 'Sunny'.sunny_days
to filter the original weather
vector, selecting only the 'Sunny' days.weather <- c('Sunny', 'Rainy', 'Cloudy', 'Sunny')
weather_factor <- factor(weather)
sunny_days <- weather_factor == 'Sunny'
sunny_weather <- weather[sunny_days]
print(sunny_weather)
[1] "Sunny" "Sunny"
In this example,
grades
which contains the values 'A'
, 'B'
, 'C'
, and 'B'
. This vector represents different grade levels.factor()
function to convert the grades
vector into a factor. We assign the result to a variable named grades_factor
. The factor()
function identifies the unique levels of the vector and converts it into a factor with those levels.ifelse()
function. This function allows us to perform element-wise conditional checks and apply different values based on the condition.grade_points
where we assign 4 points for grade 'A', 3 points for grade 'B', and 2 points for grade 'C'. This is done using the ifelse()
function to check the condition for each grade level.grade_points
.grade_points
vector to the console to see the points assigned based on the grades. This allows us to verify that the conditional operations have been performed correctly.grades <- c('A', 'B', 'C', 'B')
grades_factor <- factor(grades)
grade_points <- ifelse(grades_factor == 'A', 4, ifelse(grades_factor == 'B', 3, 2))
print(grade_points)
[1] 4 3 2 3
In this example,
responses
which contains the values 'Yes'
, 'No'
, 'No'
, and 'Yes'
. This vector represents different survey responses.factor()
function to convert the responses
vector into a factor. We assign the result to a variable named responses_factor
. The factor()
function identifies the unique levels of the vector and converts it into a factor with those levels.as.character()
function. This step is necessary to allow easy modification of the data.ifelse()
function to change 'No' responses to 'Maybe'. This function checks each element and replaces 'No' with 'Maybe'.factor()
function and assign the result to a variable named modified_responses
.modified_responses
factor to the console to see the updated survey responses. This allows us to verify that the conditional modification has been performed correctly.responses <- c('Yes', 'No', 'No', 'Yes')
responses_factor <- factor(responses)
responses_char <- as.character(responses_factor)
responses_char[responses_char == 'No'] <- 'Maybe'
modified_responses <- factor(responses_char)
print(modified_responses)
[1] Yes Maybe Maybe Yes Levels: Maybe Yes
In this tutorial, we learned How to Use Factors in Conditional Statements in R language with well detailed examples.