【问题标题】:Identify unique observations that satisfy two conditions and then remove R识别满足两个条件的唯一观测值,然后删除 R
【发布时间】:2015-09-02 10:02:04
【问题描述】:

我有一个df如下:

data
   names  fruit
7   john  apple
13  john orange
14  john  apple
2   mary orange
5   mary  apple
8   mary orange
10  mary  apple
12  mary  apple
1    tom  apple
6    tom  apple

我想做两件事。首先,计算同时具有苹果和橙子的独特观察的数量(即 2 玛丽和约翰)。

然后,我想从我的数据框中删除它们,这样我就只剩下只有苹果的独特个人了。

这是我尝试过的

toremove<-unique(data[data$fruit=='apple' & data$fruit=='orange',"names"])  ##this part doesn't work, if it had I would have used the below code to remove the names identified
data2<-data[!data$names %in% toremove,]

真的,我想使用 grepl,因为我的真实数据比水果复杂一点。这是我尝试过的(先转换为data.table)

data1<-data.table(data1)
z<-data1[,ind := grepl('app.*? & orang.*?', fruit), by='names']  ## this works fine when i just use 'app.*?' but collapses when I try to add the & sign, so I'm making an error with the operator. In addition the by='names' doesn't work out for me, which is important. My plan here was to create an indicator (if an individual has an apple and an orange, then they get an indicator==1 and I would then filter them out on the basis of this indicator). 

所以,总而言之,我的问题在于识别同时拥有苹果和橙子的人。这看起来很简单,所以请随时将我引导到可以教我这个的资源!

期望的输出

names fruit
1   tom apple
6   tom apple

【问题讨论】:

  • 所以你想要的输出不包括独特观察的计数?那么,为什么要首先计算呢?
  • 我想计算有多少人同时拥有苹果和橙子。如果我有一个针对这些条件的指标,我将对其进行子集化并计算它们。或者,我可以将我的 df 限制为只有苹果和橙子的人,并计算其中独特的人的数量。然后当我得到只有苹果人的最终输出时,一个简单的减法会告诉我有多少人同时拥有橙子和苹果。所以真的,我专注于如何识别苹果和橙子的人。在那之后我可以计算出如何计算它们。

标签: r operators data.table grepl


【解决方案1】:

如果您只查找带有apples 的名称,这里有一个简单的data.table 方法

setDT(data)[ , if(all(fruit == "apple")) .SD, by = names]
#    names fruit
# 1:   tom apple
# 2:   tom apple

对于同时具有“apple”和“orange”计数的独特观察,您可以执行类似的操作

data[, any(fruit == "apple") & any(fruit == "orange"), by = names][, sum(V1)]
## [1] 2 

最后,如果您要寻找的只是只有一个唯一 fruit 的用户,您可以尝试使用 devel version on GH(或 length(unique()))中的 uniqueN 进行条件化

data[, if(uniqueN(fruit) < 2L) .SD, by = names]
#    names fruit
# 1:   tom apple
# 2:   tom apple

【讨论】:

  • 第二行代码很棒,谢谢。第一个很好很简单,但是我的数据太复杂而无法使用。我修改了你的第二行代码看起来像这样,它似乎工作: data[, any(grepl('app.*?', fruit)) & any(grepl('orang.*?', fruit)), by = 名字]
  • 因此您也可以将第一行调整为 setDT(data)[ , if(sum(grepl("apple", fruit)) == .N) .SD, by = names] 之类的内容,以获得所需的输出
【解决方案2】:

我正在使用 dplyr 包来标记/发现有橙子的用户和有两种水果的用户。 (我在最后添加了一个额外的行来获得一个只有橙色的案例)。

data = 
  read.table(text="
             names  fruit
             7   john  apple
             13  john orange
             14  john  apple
             2   mary orange
             5   mary  apple
             8   mary orange
             10  mary  apple
             12  mary  apple
             1    tom  apple
             6    tom  apple
             21  kathy orange", header=T)

#    names  fruit
# 7   john  apple
# 13  john orange
# 14  john  apple
# 2   mary orange
# 5   mary  apple
# 8   mary orange
# 10  mary  apple
# 12  mary  apple
# 1    tom  apple
# 6    tom  apple
# 21 kathy orange


library(dplyr)

data %>%
  group_by(names) %>%                            # for each user name
  mutate(N_dist = n_distinct(fruit),             # count distinct number of fruits
         N_oranges = sum(fruit=="orange")) %>%   # count number of oranges
  filter(N_oranges == 0 & N_dist < 2) %>%        # keep users with no oranges and no both fruits
  select(names, fruit)


#   names fruit
# 1   tom apple
# 2   tom apple

请注意,在应用过滤器之前,您的数据集如下所示:

#    names  fruit N_dist N_oranges
# 1   john  apple      2         1
# 2   john orange      2         1
# 3   john  apple      2         1
# 4   mary orange      2         2
# 5   mary  apple      2         2
# 6   mary orange      2         2
# 7   mary  apple      2         2
# 8   mary  apple      2         2
# 9    tom  apple      1         0
# 10   tom  apple      1         0
# 11 kathy orange      1         1

您可以从中获取具有水果的唯一名称,或具有橙子的用户。

【讨论】:

  • 您假设这里的值始终是appleorange。更不用说这种方法对于只有orange的人会失败
  • 如果还有其他水果,代码将排除拥有 3 个水果中的 2 个的用户。该过滤器(n> 1)完成了这项工作。但是,确实是基于用户提供的具体示例。
  • 为什么只有橙色会失败?
  • 如果某个名字有only "orange",n() 将是 1,它会返回误报
  • @DavidArenburg 我仍然有一种感觉,用户只是想摆脱同时拥有两种水果的人。他说“……只有苹果”这一事实可能只是指他的具体例子。不确定他是否只想排除仅含橙子的病例。他能澄清一下吗?
猜你喜欢
  • 2023-01-25
  • 1970-01-01
  • 2020-01-09
  • 1970-01-01
  • 1970-01-01
  • 2013-08-07
  • 2020-09-08
  • 1970-01-01
相关资源
最近更新 更多