根据另一列(对于某些行)更改列的值

时间:2015-03-02 14:48:07

标签: r

我有一个数据帧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

我想将列“subategory”的名称分配给所有行的“category”列,其中category ==“Shirts”

我的结果应如下所示:

ID  category   subcategory
1   V-Neck     V-Neck
2   V-Neck     V-Neck
3   Round      Round
4   Sweaters   Striped
5   Sweaters   Striped

我试过

library(dplyr)
res <- df %>% 
  filter(category=="Shirts") %>% 
  mutate(category=subcategory)

并且结果接近我要查找的内容,但它不包含第4行和第5行。

有人可以帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:5)

不确定您是否正在寻找&#34; dplyr&#34;只回答(你的问题没有标记为这样)但这对于&#34; data.table&#34;非常简单:

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

我想用&#34; dplyr&#34;,你可以尝试ifelse(可能还有replace):

df[-1] <- lapply(df[-1], as.character)  ## Convert factors to characters
df %>%
  mutate(category = ifelse(category == "Shirts", 
                           subcategory, category))

答案 1 :(得分:1)

我知道它可能很长一段时间(并且远不如Amanda Mahto的解决方案那么优雅)但您可以将类别和子类别列转换为字符,进行索引和更新,然后转换他们回到了一个因素。

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)

答案 2 :(得分:1)

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