在R中循环以查找上一个匹配项

问题描述 投票:1回答:1

我需要一些帮助来在R中编写一个循环函数。我有一些问题,当相同的id出现时选择上一个匹配,然后写OLD_RANK列和NEW_RANK列。

OLD_RANK必须是之前比赛的NEW_RANK

`NEW_RANK`<- OLD_RANK+0.05(S1-S2)

这是我这个例子的数据

JUNK<- matrix(c(1,1,10,20,3,2,30,40,1,3,60,4,3,
4,5,40,1,5,10,30,7,6,20,20),ncol=4,byrow=TRUE)
colnames(JUNK) <- c("ID1","DAY","S1","S2")
JUNK<- as.data.frame(JUNK)

我认为可能是一个好的开始:

#subset to find previous match. Find matches before days and if more matches are 
#found, choose the row with higher values in `days`
loop for each row
s1 <- subset(s1, DAYS < days)
s1 <- subset(s1, DAYS = max(days))

#if no match fuond JUNK$OLD_RANK<-35 and JUNK$NEW_RANK <-JUNK$OLD_RANK+0.05(S1-S2)
#if previous match is found JUNK$NEW_RANK <-JUNK$OLD_RANK+0.05(S1-S2)

预期结果:

ID1    DAYS    S1     S2     OLD_RANK   NEW_RANK
1        1     10      20      35        34.5
3        2     30      40      35        34.5
1        3     60      4       34.5      37.3
3        4     5       40      34.5      32.75
1        5     10      30      37.3      36.3
7        6     20      20      35        35

任何帮助都很感激。

r database loops dataframe
1个回答
2
投票

这是一种方法:

library(dplyr)
JUNK2 <- JUNK %>%
  group_by(ID1) %>%
  mutate(change   = 0.05*(S1-S2),
         NEW_RANK = 35 + cumsum(change),
         OLD_RANK = lag(NEW_RANK) %>% if_else(is.na(.), 35, .)) %>%
  ungroup()   # EDIT: Added to end with ungrouped table

结果:

JUNK2
# A tibble: 6 x 7
    ID1   DAY    S1    S2 change NEW_RANK OLD_RANK
  <dbl> <dbl> <dbl> <dbl>  <dbl>    <dbl>    <dbl>
1     1     1    10    20  -0.5      34.5     35  
2     3     2    30    40  -0.5      34.5     35  
3     1     3    60     4   2.8      37.3     34.5
4     3     4     5    40  -1.75     32.8     34.5
5     1     5    10    30  -1        36.3     37.3
6     7     6    20    20   0        35       35  
© www.soinside.com 2019 - 2024. All rights reserved.