通过变量运行ifelse语句

时间:2018-06-22 13:55:00

标签: r

我正在尝试通过by组运行ifelse语句,但看不到如何在R中执行。

例如,如果我有

ID  ORDER
1   1
2   1
3   1
3   2
3   3
6   1
7   1
7   2

我想创建一个列,如果order = max(order)则为1,否则为每个ID给出0。因此总体而言,

1 1 0 0 1 1 0 1

因此,我的ifelse陈述是

ifelse(ORDER == max(ORDER), 1, 0)

如何为每个ID变量执行此操作(最好没有for循环)?

谢谢。

3 个答案:

答案 0 :(得分:5)

base R中,您可以使用ave()进行分组并执行

with(df, ave(ORDER, ID, FUN = function(x) x == max(x)))
#[1] 1 1 0 0 1 1 0 1

感谢@RichScriven。

数据

df <- structure(list(ID = c(1L, 2L, 3L, 3L, 3L, 6L, 7L, 7L), ORDER = c(1L, 
1L, 1L, 2L, 3L, 1L, 1L, 2L)), .Names = c("ID", "ORDER"), class = "data.frame", row.names = c(NA, 
-8L))

答案 1 :(得分:1)

library('data.table')
setDT(df1) # make df1 as data.table by reference
# check for the condition and convert it to integer
df1[, m_ord := as.integer(ORDER == max(ORDER)), by = .(ID)]  
df1

#    ID ORDER m_ord
# 1:  1     1     1
# 2:  2     1     1
# 3:  3     1     0
# 4:  3     2     0
# 5:  3     3     1
# 6:  6     1     1
# 7:  7     1     0
# 8:  7     2     1

数据:

df1 <- read.table(text='ID  ORDER
1   1
                  2   1
                  3   1
                  3   2
                  3   3
                  6   1
                  7   1
                  7   2', header = TRUE, stringsAsFactors = FALSE)

答案 2 :(得分:1)

这是不需要任何分组的另一个想法。您首先要订购数据框,然后找到重复项,即

df1 <- df[order(df$ID, df$ORDER),]
as.integer(!duplicated(df1$ID, fromLast = TRUE))
#[1] 1 1 0 0 1 1 0 1

注意::以上方法假定最大值仅出现一次(感谢Ryan进行评论)

相关问题