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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:58:55+00:00 2026-05-29T06:58:55+00:00

i would like to categorize a column in a dataframe based on a comparison

  • 0

i would like to categorize a column in a dataframe based on a comparison of subsequent rows.

for:

DF <- data.frame(respondent=rep(letters[1:2], each=5), response=c(1,1,2,2,1,3,1,1,1,1))
   respondent response
1           a        1
2           a        1
3           a        2
4           a        2
5           a        1
6           b        3
7           b        1
8           b        1
9           b        1
10          b        1

I would like to add a new column (eg: check) that turns to 1 if response in row1 = response in row2 and 0 in case they are not the same. This should be done separately for each respondent.

This would give me

   respondent response check
1           a        1  1
2           a        1  0
3           a        2  1
4           a        2  0
5           a        1  
6           b        3  0
7           b        1  1
8           b        1  1
9           b        1  1
10          b        1

I think i can figure this out with a foor loop but it seems to be a suitable problem for ddply … I just do not see how to address comparisons over adjacent rows …

  • 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-29T06:58:56+00:00Added an answer on May 29, 2026 at 6:58 am

    This is a good candidate for plyr since it splits a data and applies a function then returns the data. In this case, you need to consider the whole response vector and a similar vector shifted by one.

    The way I have approached this problem in the past is:

    shift.func <- function(df) {
      lag<-c(df$response[-1],0)
      df$check <- ifelse(df$response==lag,1,0)
    
      return(df)
    }
    
    ddply(DF,.(respondent),shift.func)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a SQL query that I would like to add a column
I have to group some data into categories, based on the column qualifier that
I am developing a website that has nested categories. I would like the categories
I would like to be able to massage certain categories of news feeds to
Would like to get a list of advantages and disadvantages of using Stored Procedures.
Would like to create a strong password in C++. Any suggestions? I assume it
Would like to be able to set colors of headings and such, different font
Would like to know what a programmer should know to become a good at
Would like to make anapplication in Java that will not automatically parse parameters used
Would like to know the c# code to actually retrieve the IP type: Static

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.