0

Med student trying to analyze and visualize some patient data in R, now in need of assistance.

I have several lists (patients in different study groups), and each list contains a column with dates. I've converted those dates to differences in days using difftime, with the first date being day 0. I'd like to generate matrices (or maybe data frames?) with the number of rows equal to the total number of days elapsed in each list, and I'll fill in the remaining columns with data that pertains to those dates.

For example, list1 is a list of 6 patients, and the $DT column is my dates (time differences in days).

list1[[6]]$DT
Time differences in days
0   36  321  405  526  532  925 1325 1521 1828 1996 2101 2166 2696

The goal is to generate a matrix for each object in that list that will eventually look like this:

matrix6
     [Date] [Lab A] [Lab B]
[48,]   48    1.23    4.56
[49,]   NA     NA      NA
.....
[60,]   60    7.89    9.01

I can make this for a single patient using a for loop, but I don't know how to iterate it across all patients on all of my lists.

Here's the for loop that works for a single patient:

nums <- as.numeric(list1[[6]]$DT)
timeline <- matrix(nrow=max(nums,na.rm=TRUE), ncol=3)
for (i in 1:length(list1[[6]]$DT){
  timeline[nums[i],1] <- nums[i]
  timeline[nums[i],2] <- list1[[6]]$labA[i]
  timeline[nums[i],3] <- list1[[6]]$labB[i]
}

This will output the following:

[1,]   NA   NA   NA
[2,]   2    NA   1.23
[3,]   NA   NA   NA
...
[47,]   NA   NA   NA
[48,]   48  4.56  NA
[49,]   NA   NA   NA
...
[66,]   NA   NA   NA

Any advice on how to generate these timelines using all the patients in a list (so list1[[1 to 6]])? I feel like I might need another for loop in there, but I can't quite get it to work right.

This is my first time using any sort of programming language, so feel free to point out anything else that looks funny!

  • 3
    A reproducible example with expected output would really help, you want a list of matrices right ? it looks like that'd be something like `lapply(list1, function(x) matrix(c(as.numeric(x$DT), x$labA, x$labB),ncol=3))` – moodymudskipper Jul 17 '18 at 18:50
  • I think that lapply is the right idea, but I'm looking to make the matrix have the same number of rows as the max value of my dates. So we'll have a row for each day in the timeline, lab values on the days that they were taken, and all other lab values will just be NA. I'll edit the post to reflect that better. – Joe O'Keefe Jul 17 '18 at 18:57
  • This might help: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?s=1|1329.6865 – moodymudskipper Jul 17 '18 at 19:11

0 Answers0