R 如何在数据框列中找到数值元素向量的第一个匹配项?

R 如何在数据框列中找到数值元素向量的第一个匹配项?,r,dataframe,vector,subset,R,Dataframe,Vector,Subset,我有一个数据帧(min_set_obs),它包含两列:第一列包含数值,称为治疗,第二列包含id列,称为seq: min_set_obs Treatment seq 1 29 1 23 3 60 1 6 2 41 1 5 2 44 假设我有一个数值向量,叫做key: key [1] 1 1 1 2 2 3 即三个1、两个2和一个3的向量 我如何从我的min\u set

我有一个数据帧(min_set_obs),它包含两列:第一列包含数值,称为治疗,第二列包含id列,称为seq:

min_set_obs
 Treatment seq
       1   29
       1   23
       3   60
       1   6
       2   41
       1   5
       2   44
假设我有一个数值向量,叫做
key

key
[1] 1 1 1 2 2 3
即三个1、两个2和一个3的向量

我如何从我的
min\u set\u obs
数据框中识别哪些行包含
向量中第一次出现的值

我希望我的输出如下所示:

Treatment seq
   1   29
   1   23
   3   60
   1   6
   2   41
   2   44
也就是说,
min\u set\u obs
中的第六行是“额外的”(当只有三个1时,它是第四个1),因此它将被删除

我熟悉%运算符中的
%,但我认为它不能告诉我
向量第一次出现在
min\u set\u obs
数据帧的第一列中的位置


感谢使用
dplyr
,您可以首先使用
计算
,然后从每组中相应地取前n行:

library(dplyr)
m <- table(key)

min_set_obs %>% group_by(Treatment) %>% do({
    # as.character(.$Treatment[1]) returns the treatment for the current group
    # use coalesce to get the default number of rows (0) if the treatment doesn't exist in key
    head(., coalesce(m[as.character(.$Treatment[1])], 0L))
})

# A tibble: 6 x 2
# Groups:   Treatment [3]
#  Treatment   seq
#      <int> <int>
#1         1    29
#2         1    23
#3         1     6
#4         2    41
#5         2    44
#6         3    60
库(dplyr)
m%(治疗组)%>%do({
#as.character(.$Treatment[1])返回当前组的处理方法
#如果关键字中不存在处理,请使用coalesce获取默认行数(0)
头部(,聚结(m[作为字符(.$Treatment[1])],0L))
})
#一个tibble:6x2
#组:治疗组[3]
#治疗顺序
#       
#1         1    29
#2         1    23
#3         1     6
#4         2    41
#5         2    44
#6         3    60

使用
dplyr
,您可以首先使用
计算
,然后从每组中相应地取前n行:

library(dplyr)
m <- table(key)

min_set_obs %>% group_by(Treatment) %>% do({
    # as.character(.$Treatment[1]) returns the treatment for the current group
    # use coalesce to get the default number of rows (0) if the treatment doesn't exist in key
    head(., coalesce(m[as.character(.$Treatment[1])], 0L))
})

# A tibble: 6 x 2
# Groups:   Treatment [3]
#  Treatment   seq
#      <int> <int>
#1         1    29
#2         1    23
#3         1     6
#4         2    41
#5         2    44
#6         3    60
库(dplyr)
m%(治疗组)%>%do({
#as.character(.$Treatment[1])返回当前组的处理方法
#如果关键字中不存在处理,请使用coalesce获取默认行数(0)
头部(,聚结(m[作为字符(.$Treatment[1])],0L))
})
#一个tibble:6x2
#组:治疗组[3]
#治疗顺序
#       
#1         1    29
#2         1    23
#3         1     6
#4         2    41
#5         2    44
#6         3    60

这里有一个带有
基本R
的选项,我们
通过“处理”将“最小设置”分解为
列表
,使用相应的“键”频率和
rbind
列表
元素分解为单个
数据帧

res <- do.call(rbind, Map(head, split(min_set_obs, min_set_obs$Treatment), n = table(key)))
row.names(res) <- NULL
res
#   Treatment seq
#1         1  29
#2         1  23   
#3         1   6
#4         2  41
#5         2  44
#6         3  60

res这里有一个带有
base R
的选项,我们
通过“治疗”将“最小设置对象”分割成一个
列表
,使用相应的“键”频率和
rbind
列表
元素分割成单个
数据帧

res <- do.call(rbind, Map(head, split(min_set_obs, min_set_obs$Treatment), n = table(key)))
row.names(res) <- NULL
res
#   Treatment seq
#1         1  29
#2         1  23   
#3         1   6
#4         2  41
#5         2  44
#6         3  60
res