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

  • Home
  • SEARCH
  • 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 9135731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:51:42+00:00 2026-06-17T08:51:42+00:00

In a data management step of my analyses I incurred into the following problem.

  • 0

In a data management step of my analyses I incurred into the following problem.

In practice, each id is recorded up to 5 times, and I have a time-varying variable of interest, tv = 1, 2, 3, 4. Suppose my data are:

dat <- read.table(text = "

        id      tv    
        1       2
        1       2
        1       1
        1       4
        2       4
        2       1
        2       4
        3       1
        3       2
        3       3
        3       3
        3       2", 

    header=TRUE)  

What I need to do is to create two newly sets of variables starting from tv, in order to obtain:

   id     tv     tv1   tv2   tv3   tv4   tv5    dur1  dur2  dur3  dur4  dur5 
    1      2      2     1     4     0     0       2     1     1     0     0
    1      2      2     1     4     0     0       2     1     1     0     0
    1      1      2     1     4     0     0       2     1     1     0     0
    1      4      2     1     4     0     0       2     1     1     0     0
    2      4      4     1     4     0     0       1     1     1     0     0
    2      1      4     1     4     0     0       1     1     1     0     0
    2      4      4     1     4     0     0       1     1     1     0     0
    3      1      1     2     3     2     0       1     1     2     1     0
    3      2      1     2     3     2     0       1     1     2     1     0
    3      3      1     2     3     2     0       1     1     2     1     0
    3      3      1     2     3     2     0       1     1     2     1     0
    3      2      1     2     3     2     0       1     1     2     1     0

For each id, in tv1–tv5 we have the ordered sequence of distinct (non-repeated) records of tv, while in dur1–dur5 we have the number of times the respective distinct records are present in the original dataset dat.

I really don’t know how to proceed here.. Any help will be greatly appreciated.

  • 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-17T08:51:43+00:00Added an answer on June 17, 2026 at 8:51 am

    This should do it:

    require(plyr)
    dat <- structure(list(id = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 
             3L, 3L), tv = c(2L, 2L, 1L, 4L, 4L, 1L, 4L, 1L, 2L, 3L, 3L, 2L
             )), .Names = c("id", "tv"), class = "data.frame", row.names = c(NA, 
             -12L))
    
    out <- ddply(dat, .(id), function(x) {
        this.rle <- rle(x$tv)
    
        val <- this.rle$values
        val <- c(val, rep(0, 5-length(val)))
        val <- matrix(rep(val,nrow(x)), byrow=T, nrow=nrow(x))
        val <- as.data.frame(val)
        names(val) <- paste("tv", 1:5, sep="")
    
        len <- this.rle$lengths
        len <- c(len, rep(0, 5-length(len)))
        len <- matrix(rep(len,nrow(x)), byrow=T, nrow=nrow(x))
        len <- as.data.frame(len)
        names(len) <- paste("dur", 1:5, sep="")
        cbind(data.frame(tv=x$tv), val, len)
    })
    
    > out
       id tv tv1 tv2 tv3 tv4 tv5 dur1 dur2 dur3 dur4 dur5
    1   1  2   2   1   4   0   0    2    1    1    0    0
    2   1  2   2   1   4   0   0    2    1    1    0    0
    3   1  1   2   1   4   0   0    2    1    1    0    0
    4   1  4   2   1   4   0   0    2    1    1    0    0
    5   2  4   4   1   4   0   0    1    1    1    0    0
    6   2  1   4   1   4   0   0    1    1    1    0    0
    7   2  4   4   1   4   0   0    1    1    1    0    0
    8   3  1   1   2   3   2   0    1    1    2    1    0
    9   3  2   1   2   3   2   0    1    1    2    1    0
    10  3  3   1   2   3   2   0    1    1    2    1    0
    11  3  3   1   2   3   2   0    1    1    2    1    0
    12  3  2   1   2   3   2   0    1    1    2    1    0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm importing massive data into a JackRabbit JCR repository. A good UI management tool
Data from another system is replicated into a SQL Server 2005 database in real-time
Currently I have a background thread that does some periodic heavy data management. Because
We have Oracle Hyperion Master Data Management (aka Data Relationship Management) installed along with
I have a parent class that handles all the UITableview persistent data management and
I have a backend data management console written with CakePHP that allows a user
I am trying to build a data management system online using mysql/php/javascript/etc. I have
The browser-based software StudyTRAX ( http://wiki.studytrax.com ), used for research data management, allows for
What are all the knowledge management features of Facebook's HiPal data analytics tool, and
I am working on a content management application in which the data being stored

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.