Search code examples
rhierarchical-datapanel-data

How to view a specific ID number using the View function in R?


I have this very basic question that could not find elsewhere, i have a data with a couple of millions of people who were followed over time. I would like to check using the View function the person who has the id number 505233. Or for instance i would like to check people that are in the 2nd and 3rd country specifically excluding the other countries.

I know that this code: View(df[, c("id", "country", "health")]) gives me back the variables that i am interested in, but what about more details inside the variable itself, could someone please guide me?

id  country health
12442   1   8
366453  2   9
366453  2   8
505233  3   8
505233  3   10


structure(list(id = structure(c(12442, 366453, 366453, 505233, 
505233), format.stata = "%9.0g"), country = structure(c(1, 2, 
2, 3, 3), format.stata = "%9.0g"), health = structure(c(8, 9, 
8, 8, 10), format.stata = "%9.0g")), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))

Solution

  • You can use the following:

    View(df[df$id == 505233, c("id", "country", "health")])
    

    By adding a statement prior to the comma within the square brackets, you can filter the data frame prior to Viewing it

    An alternative using the tidyverse would be the following

    library(dplyr)
    df %>% 
      select(id, country, health) %>% 
      filter(id == 505233) %>% 
      View()
    

    Some might prefer this if they find it more readable