+ Reply to Thread
Results 1 to 4 of 4

Thread: Moving rows

  1. #1
    Points: 10,505, Level: 68
    Level completed: 14%, Points required for next Level: 345

    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Moving rows




    I have the following dataset:
    ID HIV Status Date
    1 FALSE 22-12-99
    1 TRUE 22-12-99
    2 FALSE 22-12-99
    2 TRUE 22-12-99
    2 TRUE 21-12-99
    3 FALSE 22-12-99
    3 TRUE 22-12-99
    3 TRUE 21-12-99


    I want to move a row that has "HIV Status" equals to 'FALSE' to the same row as it is equal to 'TRUE' for the same ID. Also, if I have two IDs (e.g., ID=2) have the same HIV Status that are 'TRUE', I want to only keep the most recent date or row.

    So the output that I want is this:

    ID HIV Status Date ID HIV Status Date
    1 TRUE 22-12-99 1 FALSE 22-12-99
    2 TRUE 22-12-99 2 FALSE 22-12-99
    3 TRUE 22-12-99 3 FALSE 22-12-99


    How would I proceed to write a script in R ? I would need to write a statement that checks HIV status and Date variables before I can run the reshape command. Thanks in advance for your help.

  2. #2
    Points: 7,706, Level: 58
    Level completed: 78%, Points required for next Level: 44

    Posts
    157
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Re: Moving rows

    Step 1
    order()
    Step 2
    create a help column that indicates what to with the line using first() and last()
    Step 3
    Create two help data sets that contaib the left and the right part of your output.
    Step 4
    Merge the help data sets by
    dfnew= data.frame(dfhelp1, dfhelp2)
    Prediction is very difficult, especially about the future. (Niels Bohr)

  3. #3
    Probably A Mammal
    Points: 30,730, Level: 100
    Level completed: 0%, Points required for next Level: 0
    bryangoodrich's Avatar
    Location
    Sacramento, California, United States
    Posts
    2,561
    Thanks
    398
    Thanked 618 Times in 551 Posts

    Re: Moving rows

    Sounds like you want to split the data by HIV status, reduce them to the maximum date by ID, and then join them together by id. While that's functionally how it could be done in, say, a relational (database) schema, the Tidyverse offers some convenient ways to do this

    Code: 
    library(dplyr)  # For reducing (grouping) your data
    library(tidyr)  # For spreading (pivoting) your data out
    x <- ... your data with columns (id, status, date)
    x <- x %>% group_by(id, status) %>% summarise(date = max(date))  # reduce to one id + status per row with the max relative date
    x <- x %%> spread(status, date)  # Pivot status values to columns and populate with the max date we previously computed for each (id, status) pair
    # Or
    x <- [your data] %>%
        group_by(id, status) %>%
        summarise(date = max(date)) %>%
        spread(status, date)
    You should definitely use jQuery. It's really great and does all things.

  4. #4
    Points: 10,505, Level: 68
    Level completed: 14%, Points required for next Level: 345

    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Moving rows


    Thanks very much for your suggestions. I will try these.

+ Reply to Thread

           




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats