根据R中其他列中的值随机更新列值

根据R中其他列中的值随机更新列值,r,replace,conditional-statements,R,Replace,Conditional Statements,我想添加一个新列子类别,其中的值根据类别列的值随机填充。详情如下: Sub_Hair = c("Shampoo", "Conditioner", "Gel", "HairOil", "Dye") Sub_Beauty = c("Face", "Eye", "Lips") Sub_Nail= c("NailPolish", "NailPolishRemover", "NailArtKit", "ManiPadiKit") Sub_Others = c("Electric", "NonElectri

我想添加一个新列
子类别
,其中的值根据
类别
列的值随机填充。详情如下:

Sub_Hair = c("Shampoo", "Conditioner", "Gel", "HairOil", "Dye")
Sub_Beauty = c("Face", "Eye", "Lips")
Sub_Nail= c("NailPolish", "NailPolishRemover", "NailArtKit", "ManiPadiKit")
Sub_Others = c("Electric", "NonElectric")

> product_data_1[1:10, c("Pcode", "Category", "MRP")]
    Pcode Category    MRP
1  16156L   Beauty  $8.88
2  16162M   Others $21.27
3  16168M   Others  $2.98
4  16169E     Nail $26.64
5  16207A     Hair  $6.38
6  17012B   Beauty $33.03
7  17012C   Beauty $20.58
8  17012F   Beauty $36.29
9  17091A     Nail $20.55
10 17107D     Nail $28.20
我正在尝试下面的代码。但是,每个类别只更新一个子类别的行。例如,对于所有具有“Beauty”类别的行,子类别为“Eye”,而不是从“Face、Eye和Lips”中随机选择的值。以下是代码和输出:

product_data_1 = within(product_data_1, SubCategory[Category == "Beauty"] <- sample(Sub_Beauty, 1))
product_data_1 = within(product_data_1, SubCategory[Category == "Hair"] <- sample(Sub_Hair, 1))
product_data_1 = within(product_data_1, SubCategory[Category == "Nail"] <- sample(Sub_Nail, 1))
product_data_1 = within(product_data_1, SubCategory[Category == "Others"] <- sample(Sub_Others, 1))

> product_data_1[1:10, c("Pcode", "Category", "MRP", "SubCategory")]
    Pcode Category    MRP SubCategory
1  16156L   Beauty  $8.88         Eye
2  16162M   Others $21.27    Electric
3  16168M   Others  $2.98    Electric
4  16169E     Nail $26.64  NailPolish
5  16207A     Hair  $6.38         Gel
6  17012B   Beauty $33.03         Eye
7  17012C   Beauty $20.58         Eye
8  17012F   Beauty $36.29         Eye
9  17091A     Nail $20.55  NailPolish
10 17107D     Nail $28.20  NailPolish

product\u data\u 1=in(product\u data\u 1,SubCategory[Category==“Beauty”]这是一个基本的R解决方案。它使用Hadley Wickham在本文中解释的拆分/应用/合并策略

我将把
Sub\u*
向量放在一个列表中,
Sub\u list
。小心,
split
将按
Category
对结果排序,因此列表
Sub\u list
也必须按顺序排列向量

Sub_list <- list(Sub_Beauty, Sub_Hair, Sub_Nail, Sub_Others)
sp <- split(product_data_1, product_data_1$Category)

set.seed(1234)
sp <- lapply(seq_along(sp), function(i){
  sp[[i]]$SubCategory <- sample(Sub_list[[i]], nrow(sp[[i]]), replace = TRUE)
  sp[[i]]
})
result <- do.call(rbind, sp)
result <- result[order(as.integer(row.names(result))), ]
result
#    Pcode Category    MRP       SubCategory
#1  16156L   Beauty  $8.88               Eye
#2  16162M   Others $21.27       NonElectric
#3  16168M   Others  $2.98       NonElectric
#4  16169E     Nail $26.64        NailPolish
#5  16207A     Hair  $6.38           Shampoo
#6  17012B   Beauty $33.03               Eye
#7  17012C   Beauty $20.58              Face
#8  17012F   Beauty $36.29              Lips
#9  17091A     Nail $20.55 NailPolishRemover
#10 17107D     Nail $28.20       ManiPadiKit
数据

product_data_1 <- read.table(text = "
    Pcode Category    MRP
1  16156L   Beauty  $8.88
2  16162M   Others $21.27
3  16168M   Others  $2.98
4  16169E     Nail $26.64
5  16207A     Hair  $6.38
6  17012B   Beauty $33.03
7  17012C   Beauty $20.58
8  17012F   Beauty $36.29
9  17091A     Nail $20.55
10 17107D     Nail $28.20
", header = TRUE)

product\u data\u 1这是一个基本的R解决方案。它使用Hadley Wickham在本文中解释的拆分/应用/合并策略

我将把
Sub\u*
向量放在一个列表中,
Sub\u list
。小心,
split
将按
Category
对结果排序,因此列表
Sub\u list
也必须按顺序排列向量

Sub_list <- list(Sub_Beauty, Sub_Hair, Sub_Nail, Sub_Others)
sp <- split(product_data_1, product_data_1$Category)

set.seed(1234)
sp <- lapply(seq_along(sp), function(i){
  sp[[i]]$SubCategory <- sample(Sub_list[[i]], nrow(sp[[i]]), replace = TRUE)
  sp[[i]]
})
result <- do.call(rbind, sp)
result <- result[order(as.integer(row.names(result))), ]
result
#    Pcode Category    MRP       SubCategory
#1  16156L   Beauty  $8.88               Eye
#2  16162M   Others $21.27       NonElectric
#3  16168M   Others  $2.98       NonElectric
#4  16169E     Nail $26.64        NailPolish
#5  16207A     Hair  $6.38           Shampoo
#6  17012B   Beauty $33.03               Eye
#7  17012C   Beauty $20.58              Face
#8  17012F   Beauty $36.29              Lips
#9  17091A     Nail $20.55 NailPolishRemover
#10 17107D     Nail $28.20       ManiPadiKit
数据

product_data_1 <- read.table(text = "
    Pcode Category    MRP
1  16156L   Beauty  $8.88
2  16162M   Others $21.27
3  16168M   Others  $2.98
4  16169E     Nail $26.64
5  16207A     Hair  $6.38
6  17012B   Beauty $33.03
7  17012C   Beauty $20.58
8  17012F   Beauty $36.29
9  17091A     Nail $20.55
10 17107D     Nail $28.20
", header = TRUE)

product\u data\u 1将您的子类别值放入类似
subcat\u list的列表中。将您的子类别值放入类似
subcat\u列表的列表中效果很好。感谢您提供了多个选项。我在安装tidyverse时遇到了一些问题,但已修复。两个选项都很好。感谢您提供了多个选项。我在安装tidyverse时遇到一些问题,但已修复。两个选项都可以正常工作。