我有一个表格,其中第一列包含在不同月份和年份测量的几个人的 ID。我需要知道每个人在哪几个月和哪几年被研究,并提取唯一的 ID。有人可以帮我解决这个问题吗,因为到目前为止我在 R 中工作还无法弄清楚?
让博主帮我找到解决问题的正确代码
# creating the dataset
ID <- c("3K3855","3K3021","3K3690","3K3890","3K3812","3K3855","3K3812","3K3021","3K3855")
GR <- c(17,10,17,1,5,4,5,6,17)
SST <- c(0,2,0,1,0,0,0,1,1)
YEAR <- c(2023,2023,2021,2023,2021,2023,2022,2023,2023)
MONTH <-c("Jan","Jan","Dec","Mar","Jun","Jun","Feb","Mar","Mar")
df <- data.frame(ID,GR,SST,YEAR,MONTH)
# finding months and years using split()
want <- split(df, df$ID)
want["3K3855"] # enter the desired ID value within double-quotes inside square brackets
# alternative using base R
id <- "3K3855"
paste0(df$MONTH[df$ID == id], df$YEAR[df$ID == id])
# alternative using tidyverse
library(tidyverse)
df %>% select(ID, MONTH, YEAR) %>% arrange(ID)
# extracting unique ID values with base R
unique(df$ID)
# extracting unique ID values with tidyverse
library(tidyverse)
df %>% select(ID) %>% distinct()