1

I have data organized for the R survival package, but want to export it to work in Graphpad Prism, which uses a different structure.

#Example data
Treatment<-c("A","A","A","A","A","B","B","B","B","B")
Time<-c(3,4,5,5,5,1,2,2,3,5)
Status<-c(1,1,0,0,0,1,1,1,1,1)
df<-data.frame(Treatment,Time,Status)

The R survival package data structure looks like this

Treatment Time Status
A           3     1
A           4     1
A           5     0
A           5     0
A           5     0
B           1     1
B           2     1
B           2     1
B           3     1
B           5     1

The output I need organizes each treatment as one column, and then sorts by time. Each individual is then recorded as a 1 or 0 according to its Status. The output should look like this:

Time        A             B
1                         1
2                         1
2                         1
3           1             1
4           1                         
5           0             1
5           0             
5           0             

dcast() does something similar to what I want, but it sums up the Status values and merges them into one cell for all individuals with matching Time values.

Thanks for any help!

Crawdaunt
  • 89
  • 1
  • 12
  • 2
    Try `library(tidyverse); df %>% mutate(new = row_number()) %>% spread(Treatment, Status, fill = 0) %>% select(-new)` – Sotos Dec 12 '17 at 10:41
  • Thank you so much! This is perfect. I just need to change the (,fill = 0) to (fill = ), so it returns NA in rows without a value. – Crawdaunt Dec 12 '17 at 11:29

1 Answers1

0

I ran into a weird issue when trying to implement Sotos' code to my actual data. I got the error:

Error in Math.factor(var) : ‘abs’ not meaningful for factors

Which is weird, because Sotos' code works for the example. When I checked the example data frame using sapply() it gave me the result:

> sapply(df,class)
Treatment      Time    Status 
 "factor" "numeric" "numeric"  

My issue as far as I could tell, was that my Status variable was read as numeric in my example, but an integer in my real data:

> sapply(df,class)
Treatment      Time    Status 
 "factor" "numeric" "integer" 

I loaded my data from a .csv, so maybe that's what caused the change in variable calling. I ended up changing my Status variable using as.numeric(), and then re-generating the dataframe.

Status<-as.numeric(df$Status)
df<-data.frame(Treatment, Time, Status)

And was able to apply Sotos' code to the new dataframe.

Crawdaunt
  • 89
  • 1
  • 12