Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8896861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:11:08+00:00 2026-06-15T00:11:08+00:00

I have a data frame like this: FisherID Year Month VesselID 1 2000 1

  • 0

I have a data frame like this:

FisherID    Year    Month   VesselID
1   2000    1   56
1   2000    1   81
1   2000    2   81
1   2000    3   81
1   2000    4   81
1   2000    5   81
1   2000    6   81
1   2000    7   81
1   2000    8   81
1   2000    9   81
1   2000    10  81
1   2001    1   56
1   2001    2   56
1   2001    3   81
1   2001    4   56
1   2001    5   56
1   2001    6   56
1   2001    7   56
1   2002    3   81
1   2002    4   81
1   2002    5   81
1   2002    6   81
1   2002    7   81

…and I need the number of time that ID changes per year, so the output that I want to is:

    FisherID    Year    DiffVesselUsed
1   2000    1
1   2001    2
1   2002    0

I tried to get that using aggregate():

aggregate(vesselID, by=list(FisherID,Year,Month ), length)

but what I got was:

FisherID    Year    DiffVesselUsed
1   2000    2
1   2001    1
1   2002    1

because aggregate() counted those different vessels when those only appeared in the same month. I have tried different way to aggregate without success. Any help will be deeply appreciated. Cheers, Rafael

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:11:09+00:00Added an answer on June 15, 2026 at 12:11 am

    First a question: Your expected output does’t seem to reflect what you ask for. You ask for the number of times an ID changes per year, but your expected output seems to indicate that you want to know how many unique VesselIDs are observed per year. For example, in 2000, the ID changes once, and in 2001 the ID changes twice. In both years, two unique IDs are observed.

    So to get the result you posted,

    If you’re looking for a statistic by FisherID and Year, then there’s no reason to look by Month as well. Instead, you should look at the unique values of VesselID for each combination of FisherID and Year.

    aggregate(VesselID, by = list(FisherID, Year), function(x) length(unique(x)))
    #   Group.1 Group.2 x
    # 1       1    2000 2
    # 2       1    2001 2
    # 3       1    2002 1
    

    If you really want the number of times ID changes, use the rle function.

    aggregate(VesselID, by = list(FisherID, Year),
      function(x) length(rle(x)$values) - 1)
    #   Group.1 Group.2 x
    # 1       1    2000 1
    # 2       1    2001 2
    # 3       1    2002 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data frame like this: mat.in=data.frame(site=c('A','A','A','B','B','B'), var=c('product.A','product.B','energy','product.A','product.B','energy'), year.2011=c(12,10,40,14,12,60),year.2012=c(13,11,45,25,13,65)) For every 'site' I
I have a data frame like this one: x <- data.frame(time = c('1', '2',
I have a data frame with gaps like this: Var1 Var2 Var3 1 NA
I have a data.frame that looks like this: > head(ff.df) .id pio caremgmt prev
I have a data.frame in R that looks like this: score rms template aln_id
I have a data frame in R that looks like this: > TimeOffset, Source,
I have a dataframe with numeric entries like this one test <- data.frame(x =
Suppose I have a dataframe like this one: df <- data.frame (id = c(a,
I have a data.frame of a time series of data, I would like to
I have a data frame like this: COL1 COL2 COL3 a h b f

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.