数据表和任何功能

时间:2017-05-22 16:25:22

标签: r datatable any

我有3个变种(worldplacegroup)。共有3组:1,2和3.如果任何组观察都有place==1的观察,那么我想将group的所有观察结果标记为TRUE。我一直在尝试使用函数any和内部数据表join J但它没有用。任何人都可以解释为什么以及如何做到这一点?

预期输出:第2组对place == 1没有观察,因此place1_group应为FALSE。其他群组应place1_groupTRUE

df2 <-structure(list(world = structure(c(1L, 2L, 3L, 3L, 3L, 5L, 1L, 
                                         4L, 2L, 4L), .Label = c("AB", "AC", "AD", "AE", "AF"), class = "factor"), 
                     place = c(1, 1, 2, 2, 3, 3, 1, 2, 3, 1), 
                     group = c(1,1, 1, 2, 2, 2, 3, 3, 3, 3)), .Names = c("world", "place","group"), row.names = c(NA, -10L), class = "data.frame")

df2 <- data.table(df2)
setDT(df2)
setkey(df2, group)

# Two step approach
df2[,place1:=FALSE][place==1,place1:= TRUE]
df2[,place1_group := FALSE][any(place1), place1_group := TRUE, by = group] # Not working, place1_group TRUE for all but it should be FALSE for group==2


# Inside join approach
df2[,test := "No place 1"][J(any(place1),by=group),test:="Yes Place 1", by=group] # Why it does not work for group 3?

2 个答案:

答案 0 :(得分:1)

我会使用dplyr

df.new <- df2 %>%
          group_by(group) %>%
          mutate(tf = any(place == 1))

这应该将整个群组标记为TRUEFALSE

您可能还必须

df.new <- data.table(df.new)

答案 1 :(得分:1)

df2[,place1_group:=any(place==1),group][]
#     world place group place1_group
#  1:    AB     1     1         TRUE
#  2:    AC     1     1         TRUE
#  3:    AD     2     1         TRUE
#  4:    AD     2     2        FALSE
#  5:    AD     3     2        FALSE
#  6:    AF     3     2        FALSE
#  7:    AB     1     3         TRUE
#  8:    AE     2     3         TRUE
#  9:    AC     3     3         TRUE
# 10:    AE     1     3         TRUE