If/Then R 中基于重复值的语句

If/Then statement in R based on duplicate values

我正在寻找 return 新列中的一个值,该值基于我在下面的内容。我想说的是“如果任何值在第 2-9 列中重复 5 次,那么 return 新的第 10 列中的特定值。”如果没有值重复 5 次,则 return NA。数据示例如下。

              EntryName Team1 Team2 Team3 Team4 Team5 Team6 Team7 Team8
1                a   MIN    SF    SF    SF   ATL   TOR    SF    SF
2                b   MIN    SF    SF    SF    SF    SF   DET   MIA
3                c   MIN   CWS    SF   MIA   ATL   MIA   TOR    SF
4                d    SF    SF    SF    SF   TOR   TOR    SF   MIN
5                e   MIN   TOR   ATL   ATL    SF   CIN   DET    TB

dat$newcol <- apply(dat[,-1], 1, function(z) {
  z <- sort(table(z), decreasing = TRUE)
  if (z[1] >= 5) names(z)[1] else ""[NA]
})
dat
#   EntryName Team1 Team2 Team3 Team4 Team5 Team6 Team7 Team8 newcol
# 1         a   MIN    SF    SF    SF   ATL   TOR    SF    SF     SF
# 2         b   MIN    SF    SF    SF    SF    SF   DET   MIA     SF
# 3         c   MIN   CWS    SF   MIA   ATL   MIA   TOR    SF   <NA>
# 4         d    SF    SF    SF    SF   TOR   TOR    SF   MIN     SF
# 5         e   MIN   TOR   ATL   ATL    SF   CIN   DET    TB   <NA>

数据

dat <- structure(list(EntryName = c("a", "b", "c", "d", "e"), Team1 = c("MIN", "MIN", "MIN", "SF", "MIN"), Team2 = c("SF", "SF", "CWS", "SF", "TOR"), Team3 = c("SF", "SF", "SF", "SF", "ATL"), Team4 = c("SF", "SF", "MIA", "SF", "ATL"), Team5 = c("ATL", "SF", "ATL", "TOR", "SF"), Team6 = c("TOR", "SF", "MIA", "TOR", "CIN"), Team7 = c("SF", "DET", "TOR", "SF", "DET"), Team8 = c("SF", "MIA", "SF", "MIN", "TB")), row.names = c("1", "2", "3", "4", "5"), class = "data.frame")
library(tidyverse)
df %>%
  rowid_to_column('rn') %>%
  left_join(pivot_longer(.,-c(EntryName, rn)) %>%
  group_by(rn, EntryName) %>%
  count(value) %>%
  filter(n>=5))

  rn EntryName Team1 Team2 Team3 Team4 Team5 Team6 Team7 Team8 value  n
1  1         a   MIN    SF    SF    SF   ATL   TOR    SF    SF    SF  5
2  2         b   MIN    SF    SF    SF    SF    SF   DET   MIA    SF  5
3  3         c   MIN   CWS    SF   MIA   ATL   MIA   TOR    SF  <NA> NA
4  4         d    SF    SF    SF    SF   TOR   TOR    SF   MIN    SF  5
5  5         e   MIN   TOR   ATL   ATL    SF   CIN   DET    TB  <NA> NA

查看 value 列。倒数第二个