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 3281490
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:49:17+00:00 2026-05-17T19:49:17+00:00

I´d like to count the number of changes of binary factor variable. This variable

  • 0

I´d like to count the number of changes of binary factor variable. This variable can change from time to time back and forth multiple times for every user id. Now I´d like to count he number of changes per user id to this variable over a given timespan.

The data is sorted by id,year,month,myfactor. I tried this in MySQL but had no success so far.
Is there an easy way to do it in R? I though about adding another column to my data.frame and adding up conditions step by step… Maybe some %in% stuff ?

Thx in advance for suggestions…

Hmm, of course… here´s some example – sorry for not providing it immediately, my head hurts ;):


   myf   Year    month userid   
  1 A    2005       1    260           
  2 B    2005       2    260           
  3 B    2005       4    260           
  4 A    2005       5    260           
  5 B    2005       6    260           
  6 B    2005       1    261 

if this is my dataset, I want to update the changes column, counting the number of changes of myf per user. Basically id like to end up with:

  user  changes
   260     3
   260     0

and so forth…

HTH

  • 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-05-17T19:49:18+00:00Added an answer on May 17, 2026 at 7:49 pm
    #Some data
    dfr <- data.frame(
       binary_variable = runif(100) < .7,
       id = sample(7, 100, replace = TRUE)
    )
    
    #Split by id
    split_by_id <- with(dfr, split(binary_variable, id))
    
    #Number of changes
    sapply(split_by_id, function(x) sum(diff(x) != 0))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query like this: SELECT t1.id, (SELECT COUNT(t2.id) FROM t2 WHERE t2.id
How can I count operations in C++? I'd like to analyze code in a
I had this idea of creating a count down timer, like 01:02, on the
I have a table like as follows: SoftwareName Count Country Project 15 Canada Visio
Does dot net have an interface like IEnumerable with a count property? I know
When using C++ namespaces, do you prefer to explicitly name them, like this: std::cout
Many C++ books contain example code like this... std::cout << Test line << std::endl;
Like many others on this site I am considering a move to ASP.NET MVC
I'd like to monitor an NSCountedSet to see if its contents changes. Setting up
This has always bugged the hell out of me. Why are lists like this?

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.