确定一个值是否与之前匹配的组中的 id 匹配

Identify if a value matches the id in a group it was previously matched with

我有一个数据框,其中包含按站点和 ID 分组的数据。我想创建一个新列,其中包含一个逻辑值,指示在每个站点中,id 是否与前一行中的角度匹配。

other.dat <- c(1:12)
site <- c(rep(1,6), rep(2,6))
id <- c(1.1,1.2,1.3,1.1,1.2,1.3,2.1,2.2,2.3,2.1,2.2,2.3)
angle <- c(65,250,150,65,250,150,0,240,150,345,205,100)
data.frame(other.dat,site,id,angle)

   other.dat site  id angle
1          1    1 1.1    65
2          2    1 1.2   250
3          3    1 1.3   150
4          4    1 1.1    65
5          5    1 1.2   250
6          6    1 1.3   150
7          7    2 2.1     0
8          8    2 2.2   240
9          9    2 2.3   150
10        10    2 2.1   345
11        11    2 2.2   205
12        12    2 2.3   100

不像上一个问题给出的例子 (),对于逻辑变量为 TRUE 的站点而言,angle 不需要是唯一的,并且第一个唯一 ID 将始终为 TRUE。例如:

other.dat <- c(1:12)
site <- c(rep(1,6), rep(2,6))
id <- c(1.1,1.2,1.3,1.1,1.2,1.3,2.1,2.2,2.3,2.1,2.2,2.3)
angle <- c(65,250,150,65,250,150,0,240,150,345,205,100)
same.angle <- c(T,T,T,T,T,T,T,T,T,F,F,F)
data.frame(other.dat,site,id,angle, same.angle)

   other.dat site  id angle same.angle
1          1    1 1.1    65       TRUE
2          2    1 1.2   250       TRUE
3          3    1 1.3   150       TRUE
4          4    1 1.1    65       TRUE
5          5    1 1.2   250       TRUE
6          6    1 1.3   150       TRUE
7          7    2 2.1     0       TRUE
8          8    2 2.2   240       TRUE
9          9    2 2.3   150       TRUE
10        10    2 2.1   345      FALSE
11        11    2 2.2   205      FALSE
12        12    2 2.3   100      FALSE

我试过使用 df <- within(df, same.angle <- as.logical(angle, id, FUN = function(x) length(unique(x))==1))) 但由于我还不明白的原因,只有第 7 行返回为 'FALSE'。

在此先感谢您的帮助!

试试这个。您可以按所需变量分组,然后使用 lag() 复制行比较,然后比较以达到预期输出:

library(dplyr)
#Code
new <- df %>% group_by(site,id) %>%
  mutate(Var=lag(angle),
         Same.angle=ifelse(is.na(Var),T,ifelse(angle==Var,T,F))) %>%
  select(-Var)

输出:

# A tibble: 12 x 5
# Groups:   site, id [6]
   other.dat  site    id angle Same.angle
       <int> <dbl> <dbl> <dbl> <lgl>     
 1         1     1   1.1    65 TRUE      
 2         2     1   1.2   250 TRUE      
 3         3     1   1.3   150 TRUE      
 4         4     1   1.1    65 TRUE      
 5         5     1   1.2   250 TRUE      
 6         6     1   1.3   150 TRUE      
 7         7     2   2.1     0 TRUE      
 8         8     2   2.2   240 TRUE      
 9         9     2   2.3   150 TRUE      
10        10     2   2.1   345 FALSE     
11        11     2   2.2   205 FALSE     
12        12     2   2.3   100 FALSE   

我们按 'site'、'id' 分组,将 'angle' 的 lag 与 'angle' 进行比较,方法是将 default 指定为first 'angle'

的元素
library(dplyr)
df %>% 
  group_by(site, id) %>% 
  mutate(Same.angle = lag(angle, default = first(angle)) == angle)

-输出

# A tibble: 12 x 5
# Groups:   site, id [6]
#   other.dat  site    id angle Same.angle
#       <int> <dbl> <dbl> <dbl> <lgl>     
# 1         1     1   1.1    65 TRUE      
# 2         2     1   1.2   250 TRUE      
# 3         3     1   1.3   150 TRUE      
# 4         4     1   1.1    65 TRUE      
# 5         5     1   1.2   250 TRUE      
# 6         6     1   1.3   150 TRUE      
# 7         7     2   2.1     0 TRUE      
# 8         8     2   2.2   240 TRUE      
# 9         9     2   2.3   150 TRUE      
#10        10     2   2.1   345 FALSE     
#11        11     2   2.2   205 FALSE     
#12        12     2   2.3   100 FALSE   

或使用 base Rave

df$Same.angle <- with(df, !ave(angle, site, id, 
         FUN = function(x) c(0, diff(x))))