Search code examples
rdataframelabelrow

Labelling two separate data frames by row and then merging them into one data frame under the same column but with row labels preserved


I have two data sets, lets call them

A: 1, 3, 5, 6, 3

and

B: 2, 4, 7, 9, 8

Where the numbers in A and B represent time

I want to label all of the numbers in A by row as "positive" and all numbers in B labelled "Negative" by row.

I then want to merge both A and B into a single data frame under one column called "time", but they must keep their row names "Positive"/"Negative" for their corresponding number so I can plot both onto a survival plot


Solution

  • My guess is that you are looking for such a dataframe:

    A <- data.frame(time = c(1, 3, 5, 6, 3), status= "positive")
    B <- data.frame(time = c(2, 4, 7, 9, 8), status= "negative")
    
    rbind(A, B)
    

    Output:

       time   status
    1     1 positive
    2     3 positive
    3     5 positive
    4     6 positive
    5     3 positive
    6     2 negative
    7     4 negative
    8     7 negative
    9     9 negative
    10    8 negative