简体   繁体   English

根据R中其他三列的最高值来分配列值

[英]Assigning column values based on which value is highest in a row of three other columns in R

I would like to do the following in R: 我想在R中执行以下操作:

I have purchase frequencies of three types of products: AB and C (customer being the identifier here). 我有三种产品的购买频率:AB和C(此处是客户的标识)。 Now I would like to create a fourth column that per row has a value of 0 if product A has the highest purchase frequency, a value of 1 if product B has the highest purchase frequency, and a value of 2 if product C has the highest purchase frequency. 现在,我想创建第四列,如果产品A的购买频率最高,则每行的值为0;如果产品B的购买频率最高,则为1;如果产品C的购买频率最高,则为2购买频率。 Ideally, if the frequency of purchases for two or even all three products is equally high I would like to assign one of the three values randomly. 理想情况下,如果两个或什至所有三个产品的购买频率都很高,我想随机分配这三个值之一。 Of course, this random assignment should only be done for just two of the three categories if only two categories (and not three) are highest and equal. 当然,如果只有两个类别(而不是三个类别)是最高且相等的,则只能对三个类别中的两个类别进行随机分配。

So say I have the following table: 所以说我有下表:

customer    A    B    C
1           2    3    4
2           4    6    5
3           4    2    4
4           4    2    4
5           4    4    4
6           2    2    2
7           4    4    4

I would like to create (for example) the following column: 我想创建(例如)以下列:

highest_purchase_freq
2
1
0
2
0
1
2

It would be amazing if someone could help me. 如果有人可以帮助我,那将是惊人的。

Thanks in advance! 提前致谢!

The which.is.max function of nnet could be what you are looking for ? 您正在寻找nnetwhich.is.max函数吗?
It finds the maximum position in a vector, breaking ties at random. 它找到向量中的最大位置,随机断开关系。

set.seed(12345)
library(nnet)
df$highest_purchase_freq <- apply(df[2:4], 1, which.is.max)-1
df  

  customer A B C highest_purchase_freq
1        1 2 3 4                     2
2        2 4 6 5                     1
3        3 4 2 4                     2
4        4 4 2 4                     2
5        5 4 4 4                     2
6        6 2 2 2                     2
7        7 4 4 4                     1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据其他三列中的值分配列值 - Assigning column value based on values in three other columns 根据其他列的索引对列中的值求和,然后将该值分配给每一行 - Sum Values in Column based on indexes of other columns and then assigning that value to each row R-根据其他两列的比较将值分配给一列 - R - assigning value to one column based on a comparison of two other columns 如何根据其他三列获得一列中的最大值? - How to get the highest value in a column depending on three other columns? 根据其他三列中的值添加一列 - add a column based on values in three other columns 如何根据三列删除重复项,但我使用 R 保留特定列中编号最高的行? - How do I remove duplicates based on three columns, but I keep the row with the highest number in the specific column using R? 根据其他两列的值更改 R 中一列的值 - Change the value of a column in R based on values of two other columns 根据多列中的最大值和另一列中的值 label 分配列值 - Assigning column value based on highest value across multiple columns and value label in another column 在 R 中使用向量方法根据从其他列派生的周期为列分配值 - Assigning value to a column based on periods derived from other columns using vector approach in R 根据第2列中的最大值找到第1列中值的最高组合-R - Find the highest combination of values in column 1 based on max value in column 2 - R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM