Search code examples
rcountrow

Count number of different variables in a row


I have a question regarding cleaning data/checking observations. Due to privacy issues, I cannot share the full code or data. I can illustrate what I want to achieve with an example. Suppose this is my dataset.

Individual Year
101 2018
101 2019
102 2019
103 2019
104 2017
104 2018
104 2019

And suppose I want to count the number of different individuals in this dataset. Then, in this case, it would be 4 ('101', '102', '103', '104'). But, I have this in a very extended way. Is there an easy way to check how many individuals the total dataset has?

Hopefully, somebody can help :) I think there should be an easy solution to this, but googling hasn't helped so far.


Solution

  • Here is your example data:

    data <- data.frame(Individual = c(101, 101, 102, 103, 104, 104, 104),
                       Year = c("2018", "2019", "2019", "2019", "2017", "2018", "2019"))
    

    Looks like this:

      Individual Year
    1        101 2018
    2        101 2019
    3        102 2019
    4        103 2019
    5        104 2017
    6        104 2018
    7        104 2019
    

    To count the number of unique values in a column, you can use the following code:

    length(unique(data$Individual))
    

    The output is:

    [1] 4
    

    The output is in this case 4.