Reputation: 2989
I have a dataframe df
ID <- c(1,2,3,4,5)
category <- c("Shirts", "Shirts", "Shirts", "Sweaters", "Sweaters")
subcategory <- c("V-Neck","V-Neck","Round","Striped","Striped")
df <- data.frame(ID,category,subcategory)
ID category subcategory
1 Shirts V-Neck
2 Shirts V-Neck
3 Shirts Round
4 Sweaters Striped
5 Sweaters Striped
I want to assign the name of the column "subcategory" to the column "category" for all rows, where category == "Shirts"
My result should look like this:
ID category subcategory
1 V-Neck V-Neck
2 V-Neck V-Neck
3 Round Round
4 Sweaters Striped
5 Sweaters Striped
I tried
library(dplyr)
res <- df %>%
filter(category=="Shirts") %>%
mutate(category=subcategory)
and the result is close to what I am looking for, but it does not contain the rows 4 and 5.
Can someone help me with this issue?
Upvotes: 3
Views: 10244
Reputation: 151
df$category <- ifelse(df$category=="Shirts",
as.character(df$subcategory),
as.character(df$category))
df
# ID category subcategory
# 1 1 V V
# 2 2 V V
# 3 3 Round Round
# 4 4 Sweaters Striped
# 5 5 Sweaters Striped
Upvotes: 2
Reputation: 145
I know it's probably a long way round (and nowhere near as elegant as Amanda Mahto's solution) but you can convert your category and subcategory columns to characters, do your indexing and updating, then convert them back to a factor.
df$category <- as.character(df$category); df$subcategory <- as.character(df$subcategory)
indx <- category == "Shirts" ; df$category[indx] <- df$subcategory[indx]
df$category <- as.factor(df$category); df$subcategory <- as.factor(df$subcategory)
Upvotes: 1
Reputation: 193517
Not sure if you are looking for a "dplyr" only answer (your question isn't tagged as such) but this is very straightforward with "data.table":
library(data.table)
as.data.table(df)[category == "Shirts", category := subcategory][]
# ID category subcategory
# 1: 1 V V
# 2: 2 V V
# 3: 3 Round Round
# 4: 4 Sweaters Striped
# 5: 5 Sweaters Striped
I suppose with "dplyr", you could try ifelse
(and possibly replace
):
df[-1] <- lapply(df[-1], as.character) ## Convert factors to characters
df %>%
mutate(category = ifelse(category == "Shirts",
subcategory, category))
Upvotes: 6