One or more observations for same id across rows

I have a larger dataset where I would like a new column with values 1 or 0 (or true/false) depending on values in a specific colum (species) across rows (grouped by the id)

Below an example of input:

id   id_row   number_of_tests   species       
1    1               2             avium
1    2               2             avium     
2    1               1             kansasii
3    1               1             gordonae
4    1               4             avium
4    2               4             avium
4    3               4             avium
4    4               4             gordonae
5    1               1             avium

Species may take 72 different values. Desired output is a new colum (i.e. not_same_species) that is 1 if the specie is the same for all observations for the same id or 0 if there are different observations.

id   id_row   number_of_tests   species        not_same_species         
1    1               2             avium            0
1    2               2             avium            0   
2    1               1             kansasii         0
3    1               1             gordonae         0
4    1               4             avium            1
4    2               4             avium            1
4    3               4             avium            1
4    4               4             gordonae         1
5    1               1             avium            0

Thanks in advance!

library(tidyverse)

(mydf <- tibble::tribble(
  ~id, ~id_row, ~number_of_tests,   ~species,
   1L,      1L,               2L,    "avium",
   1L,      2L,               2L,    "avium",
   2L,      1L,               1L, "kansasii",
   3L,      1L,               1L, "gordonae",
   4L,      1L,               4L,    "avium",
   4L,      2L,               4L,    "avium",
   4L,      3L,               4L,    "avium",
   4L,      4L,               4L, "gordonae",
   5L,      1L,               1L,    "avium"
  ))


mydf |> 
  group_by(id) |> 
  mutate(not_same_species = 
           as.integer(length(unique(species)) != 1))
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.