1

我想找出谁在至少 2 个不同的(唯一的)日期有一个苹果或一个橙子。我想创建一个带有二进制指示符的新列,用于指示个人在至少两个日期(1 = 是,0 = 否)是否有橙子或苹果。

我来的最近的是这个 plyr 代码。

df1<- ddply(df, .(names, fruit), mutate, acne = ifelse(fruit=="apple" | fruit=="orange" & length(unique(dates))>=2,1,0))

然而,这不是解决方案。安妮在同一天得到了两次苹果,所以她不应该在这里得到 1。同样 ted 得到 1,尽管他只得到过一次苹果。

这更接近,但仍然不正确。它对出现两次的任何水果给出 1。需要水果在每人两个单独的日期每人出现两次

df2<- ddply(df, .(fruit), mutate, acne = ifelse(length(unique(dates))>=2, 1, 0
##this one gives a 1 to any fruit that has occurred twice. Need the fruit to occur twice per person on two individual dates per person.

如果有人能在这里指出我正确的方向,我将不胜感激。

先感谢您

样本 DF

names<-as.character(c("john", "john", "philip", "ted", "john", "john", "anne", "john", "mary","anne", "mary","mary","philip","mary", "su","mary", "jim", "sylvia", "mary", "ted","ted","mary", "sylvia", "jim", "ted", "john", "ted"))
dates<-as.Date(c("2010-07-01", "2010-07-13", "2010-05-12","2010-02-14","2010-06-30","2010-08-15", "2010-03-21","2010-04-04","2010-09-01", "2010-03-21", "2010-12-01", "2011-01-01", "2010-08-12",  "2010-11-11", "2010-05-12",  "2010-12-03", "2010-07-12",  "2010-12-21", "2010-02-18",  "2010-10-29", "2010-08-13",  "2010-11-11", "2010-05-12",  "2010-04-01", "2010-05-06",  "2010-09-28", "2010-11-28" ))
fruit<-as.character(c("kiwi","apple","mango", "banana","strawberry","orange","apple","raspberry", "orange","apple","orange", "apple", "strawberry", "apple", "pineapple", "peach", "orange", "nectarine", "grape","banana", "melon", "apricot", "plum", "lychee", "mango", "watermelon", "apple" ))
df<-data.frame(names,dates,fruit)
df

期望的输出

    names      dates      fruit v1
7    anne 2010-03-21      apple  0
10   anne 2010-03-21      apple  0
17    jim 2010-07-12     orange  0
24    jim 2010-04-01     lychee  0
1    john 2010-07-01       kiwi  1
2    john 2010-07-13      apple  1
5    john 2010-06-30 strawberry  1
6    john 2010-08-15     orange  1
8    john 2010-04-04  raspberry  1
26   john 2010-09-28 watermelon  1
9    mary 2010-09-01     orange  1
11   mary 2010-12-01     orange  1
12   mary 2011-01-01      apple  1
14   mary 2010-11-11      apple  1
16   mary 2010-12-03      peach  1
19   mary 2010-02-18      grape  1
22   mary 2010-11-11    apricot  1
3  philip 2010-05-12      mango  0
13 philip 2010-08-12 strawberry  0
15     su 2010-05-12  pineapple  0
18 sylvia 2010-12-21  nectarine  0
23 sylvia 2010-05-12       plum  0
4     ted 2010-02-14     banana  0
20    ted 2010-10-29     banana  0
21    ted 2010-08-13      melon  0
25    ted 2010-05-06      mango  0
27    ted 2010-11-28      apple  0
4

3 回答 3

2

这应该可以解决问题:

 v1 = ave(1:nrow(df),df$names,FUN=function(x) length(unique(df$dates[x[df$fruit[x]
                                              %in% c("orange","apple")]]))>1)
 df$v1 = v1
 df = df[order(df$names),]
于 2013-07-19T20:42:04.370 回答
2

如果我理解正确,就您的问题而言,苹果 == 橙子。所以计划是(1)创建一个小的data.frame,其中水果只有橙子或苹果,因为你不关心其他水果,(b)只选择唯一的日期/名称行,(c)按名称聚合和(d) 合并回您的原始 data.frame 以获得您的结果:

ndf <- subset(df, fruit %in% c("apple", "orange"))
ndf <- ndf[!duplicated(ndf[, c("names", "dates")]), ]

在这里你可以使用表格,但我更喜欢聚合

v <- aggregate(rep(1, nrow(ndf)), by = ndf[, "names", drop = FALSE], sum)
v$x <- ifelse(v$x > 1, 1, 0)
rv <- merge(df, v)

从代码上看,它比其他答案要长一些,但很清楚,而且肯定能胜任。您可以在没有前两个部分的情况下只使用聚合,但如果您有巨大的 data.frame,每个名称都有大量名称聚合可能会非常昂贵。

于 2013-07-19T20:53:28.570 回答
1

我使用by. Rownames 在 期间被破坏do.call,但您可以修复它。

result <- by(df, INDICES = df$names, FUN = function(x) {
  if (length(unique(x$dates)) == 1) {
    x$index <- 0
    return(x)
  }
  ao.sum <- sum(x$fruit %in% c("apple", "orange"))
  if (ao.sum < 2) x$index <- 0 else x$index <- 1
  x
})

do.call("rbind", result)

           names      dates      fruit index
anne.7      anne 2010-03-21      apple     0
anne.10     anne 2010-03-21      apple     0
jim.17       jim 2010-07-12     orange     0
jim.24       jim 2010-04-01     lychee     0
john.1      john 2010-07-01       kiwi     1
john.2      john 2010-07-13      apple     1
john.5      john 2010-06-30 strawberry     1
john.6      john 2010-08-15     orange     1
john.8      john 2010-04-04  raspberry     1
john.26     john 2010-09-28 watermelon     1
mary.9      mary 2010-09-01     orange     1
mary.11     mary 2010-12-01     orange     1
mary.12     mary 2011-01-01      apple     1
mary.14     mary 2010-11-11      apple     1
mary.16     mary 2010-12-03      peach     1
mary.19     mary 2010-02-18      grape     1
mary.22     mary 2010-11-11    apricot     1
philip.3  philip 2010-05-12      mango     0
philip.13 philip 2010-08-12 strawberry     0
su            su 2010-05-12  pineapple     0
sylvia.18 sylvia 2010-12-21  nectarine     0
sylvia.23 sylvia 2010-05-12       plum     0
ted.4        ted 2010-02-14     banana     0
ted.20       ted 2010-10-29     banana     0
ted.21       ted 2010-08-13      melon     0
ted.25       ted 2010-05-06      mango     0
ted.27       ted 2010-11-28      apple     0
于 2013-07-19T20:43:52.773 回答