First I need to make the setosas male and the versicolors and virginica female. Then I need to swap these males and females. And I need to connect them to the loop
Burak=iris
sehir= c("zonguldak","bilecik","samsun","sinop","gender")
for ( i in 1:ncol(Burak)) {
colnames(Burak)[i] = sehir[i]
}
head(Burak)
levels(iris$Species) <- c(levels(iris$Species), "female")
iris$Species[iris$Species == 'virginica'] <- 'female'
levels(iris$Species) <- c(levels(iris$Species), "male")
iris$Species[iris$Species == 'setosa'] <- 'male'
levels(iris$Species) <- c(levels(iris$Species), "female")
iris$Species[iris$Species == 'versicolor'] <- 'female'
CodePudding user response:
Try to understand how to assign levels to a factor.
To change the "names" of your factor levels, just assign the "new names" as factor levels in the order needed.
When you check the "sequence" of the original factor levels, you get:
levels(Burak$gender)
[1] "setosa" "versicolor" "virginica"
For demonstration purposes, let's add a new column and "overwrite" the factor levels in the order you need them.
Burak$gender2 <- Burak$gender
levels(Burak$gender2) <- c("male","female","female")
Alternatively, you can use a more verbose assignment using a list.
Burak$gender3 <- Burak$gender
levels(Burak$gender3) <- list(male = "setosa", female = "versicolor", female = "virginica")