3

I have a data frame on bills that has (among other variables) a column for 'year', a column for 'issue', and a column for 'sub issue.' A simplified example df looks like this:

year   issue   sub issue 
1970   4       20
1970   3       21
1970   4       22
1970   2       8
1971   5       31
1971   4       22
1971   9       10
1971   3       21
1971   4       22

Etc., for about 60 years. I want to count the unique values in the issue and sub issue columns for each year, and use those to create a new df- dat2. Using the df above, dat2 would look like this:

year   issues    sub issues
1970    3        4
1971    4        4

Weary of factors, I confirmed that the values in all columns are integers, if that makes a difference. I am new at R (obviously), and I haven't been able to find relevant code for this specific purpose online. Thanks for any help!!

1 Answer 1

7

That's a one-liner, with aggregate:

with(d,aggregate(cbind(issue,subissue) ~ year,FUN=function(x){length(unique(x))}))

returning:

  year issue subissue
1 1970     3        4
2 1971     4        4
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.