Moving rows

#1
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
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)
 

bryangoodrich

Probably A Mammal
#3
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)