按R中的组替换条件行值

按R中的组替换条件行值,r,R,我有一张桌子 ID RATES 1 0.01 1 0 1 0 1 0 2 0.05 2 0.05 2 0.01 2 0 3 0 3 0 3 0 理想情况下,我希望创建一个名为n_rates的新列,该列将rate列中的0值替换为grou

我有一张桌子

ID         RATES
 1          0.01
 1            0
 1            0
 1            0
 2          0.05
 2          0.05
 2          0.01
 2            0
 3            0
 3            0
 3            0
理想情况下,我希望创建一个名为n_rates的新列,该列将rate列中的0值替换为group(ID)和conditions

Condition 1 - If at least one rate IN (0.01, 0.015, 0.05) for this ID THEN replace all 0 values by the most rate values (e.g. if for this ID, 0.01 appears more than 0.05 then replace 0 with 0.01. Only rates == 0 rows can be replaced)

Condition 2 - If rates NOT IN (0.01, 0.015, 0.05) then make no changes to the rows

你只需要找到模式,每个组的最高频率值,我在这里使用dplyr
group\u by

作用于


结果

dt

   ID RATES
1   1  0.01
2   1  0.01
3   1  0.01
4   1  0.01
5   2  0.05
6   2  0.05
7   2  0.05
8   2  0.01
9   3  0.00
10  3  0.00
11  3  0.00

你只需要找到模式,每个组的最高频率值,我在这里使用dplyr
group\u by

作用于


结果

dt

   ID RATES
1   1  0.01
2   1  0.01
3   1  0.01
4   1  0.01
5   2  0.05
6   2  0.05
7   2  0.05
8   2  0.01
9   3  0.00
10  3  0.00
11  3  0.00

这是一个由
%%>%do(…)

myfun%
分组依据(ID)%>%
do(myfun(.))
#一个tibble:11x2
#组别:ID[3]
#身份证费率
#   
# 1     1 0.0100
# 2     1 0.0100
# 3     1 0.0100
# 4     1 0.0100
# 5     2 0.0500
# 6     2 0.0500
# 7     2 0.0100
# 8     2 0.0500
# 9     3 0.    
# 10     3 0.    
# 11     3 0.
资料


df这里有一个由
%%>%do(…)

myfun%
分组依据(ID)%>%
do(myfun(.))
#一个tibble:11x2
#组别:ID[3]
#身份证费率
#   
# 1     1 0.0100
# 2     1 0.0100
# 3     1 0.0100
# 4     1 0.0100
# 5     2 0.0500
# 6     2 0.0500
# 7     2 0.0100
# 8     2 0.0500
# 9     3 0.    
# 10     3 0.    
# 11     3 0.
资料


df用于一行
数据。表
回答,并使用函数:

Mode <- function(x) {
    ux <- unique(x)
    ux[which.max(tabulate(match(x, ux)))]
}

library(data.table)
setDT(df)[, Rates := ifelse(Rates==0 & any(Rates!=0), 
                             Mode(Rates[Rates!=0]), Rates), by = ID]
df

#ID Rates
#1  0.01
#1  0.01
#1  0.01
#1  0.01
#2  0.05
#2  0.05
#2  0.01
#2  0.05
#3  0.00
#3  0.00
#3  0.00

模式对于一行数据
回答,使用函数:

Mode <- function(x) {
    ux <- unique(x)
    ux[which.max(tabulate(match(x, ux)))]
}

library(data.table)
setDT(df)[, Rates := ifelse(Rates==0 & any(Rates!=0), 
                             Mode(Rates[Rates!=0]), Rates), by = ID]
df

#ID Rates
#1  0.01
#1  0.01
#1  0.01
#1  0.01
#2  0.05
#2  0.05
#2  0.01
#2  0.05
#3  0.00
#3  0.00
#3  0.00
模式
df <- read.table(text="ID         RATES
 1          0.01
 1            0
 1            0
 1            0
 2          0.05
 2          0.05
 2          0.01
 2            0
 3            0
 3            0
 3            0", header=TRUE)
Mode <- function(x) {
    ux <- unique(x)
    ux[which.max(tabulate(match(x, ux)))]
}

library(data.table)
setDT(df)[, Rates := ifelse(Rates==0 & any(Rates!=0), 
                             Mode(Rates[Rates!=0]), Rates), by = ID]
df

#ID Rates
#1  0.01
#1  0.01
#1  0.01
#1  0.01
#2  0.05
#2  0.05
#2  0.01
#2  0.05
#3  0.00
#3  0.00
#3  0.00