0

I am working in R and have a list, we'll say named x. This list is multidimensional, it contains 100 different outputs of the survival::survConcordance() function (i.e. so x[[i]][j] where i is the number of different survConcordance() ouputs (i = 1 to 100) and j is the number of elements in each output (j = 1 to 5). What I want to do is for each i, compute my concordance statistic, which is a function of the output of survConcordance() according to this formula: (for each i) concordance <- (x[[i]][1]+(x[[i]][3] / 2))/(x[[i]][1]+x[[i]][2]+x[[i]][3]). However, I'm struggling to figure out how to do this. Although I'm virtually certain the solution lies somewhere within the lapply() family of functions. Any ideas?

jclifto8
  • 93
  • 1
  • 6
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jan 16 '20 at 16:59

1 Answers1

0

Difficult to know if this works without a reproducible example, but from your description of the problem, this should work:

all_concordances <- sapply(x, function(vec) (vec[1] + (vec[3]/2))/ sum(vec[1:3]))

This will give you a vector of 100 concordances.

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • This is how I first tried to do it, although it didn't work.. however, upon trying again it now works haha. So, thanks! I've got it now! – jclifto8 Jan 16 '20 at 17:18
  • Quick follow up question.. would you be able to explain the difference between ```sapply()``` and ```lapply()```? I used the latter instead of the former, as you noted in your answer, but it still worked. – jclifto8 Jan 16 '20 at 17:19
  • @jclifto8 sapply just simplifies the return of lapply, making it into a vector instead of a list. Please remember to accept the answer if it solves your problem so other users can see it has a solution. Thanks! – Allan Cameron Jan 16 '20 at 17:21