1

訪問ごとに 1 行として構造化された縦方向のデータセットがあります。
数値の患者 ID 番号は、一意の患者を示します。

データセットから観測値が 2 未満のすべての患者を削除するにはどうすればよいですか?

したがって、この例では、患者105110の行を削除します。

Patient ID   Disease Score
101             5
101             2
101             2
105             1
110             5
115             1
115             1
4

1 に答える 1

3
dat <- read.table(text="Patient ID,Disease Score
101,5
101,2
101,2
105,1
110,5
115,1
115,1", stringsAs=FALSE, header=TRUE, sep=",")

# one way in base
dat[dat$Patient.ID %in% names(which(table(dat$Patient.ID)>2)),]

# one way in dplyr
library(dplyr)

dat %>% 
  group_by(Patient.ID) %>%
  mutate(n=n()) %>%
  ungroup() %>%
  filter(n>=2) %>%
  select(Patient.ID, Disease.Score)
于 2015-01-25T23:39:24.943 に答える