0

I was hoping I could get some help. I am constructing a life table, not for insurance, but for ecology (a cross-sectional of the population of a any kind of wild fauna), so essentially censoring variables like smoker/non-smoker, pregnant, gender, health-status, etc.:

AgeClass=C(1,2,3,4,5,6)
SampleSize=c(100,99,87,46,32,19)
for(i in 1:6){
+ PropSurv=c(Sample/100)
+ }
> LifeTab1=data.frame(cbind(AgeClass,Sample,PropSurv))

Which gave me this:

ID  AgeClas  Sample  PropSurv
1    1       100       1.00
2    2        99       0.99
3    3        87       0.87
4    4        46       0.46
5    5        32       0.32
6    6        19       0.19

I'm now trying to calculate those that died in each row (DeathInt) by taking the initial number of those survived and subtracting it by the number below it (i.e. 100-99, then 99-87, then 87-46, so on and so forth). And try to look like this:

ID  AgeClas   Sample   PropSurv  DeathInt
 1     1       100       1.00       1
 2     2        99       0.99       12
 3     3        87       0.87       41
 4     4        46       0.46       14
 5     5        32       0.32       13
 6     6        19       0.19       NA

I found this and this, and I wasn't sure if they answered my question as these guys subtracted values based on groups. I just wanted to subtract values by row.

Also, just as a side note: I did a for() to get the proportion that survived in each age group. I was wondering if there was another way to do it or if that's the proper, easiest way to do it.

Second note: If any R-users out there know of an easier way to do a life-table for ecology, do let me know!

Thanks!

Community
  • 1
  • 1
Lalochezia
  • 497
  • 4
  • 15
  • Have a look at your code, the `for`-loop has no use... Just say: `sample <- c(100, 99, 87, 46, 32, 19)` and then `LifeTab1 <- data.frame(ID = 1:6, AgeClass = 1:6, Sample = sample, PropSurv = sample/100, DeathInt = c(-diff(sample), NA))` Including my answer! – David Nov 06 '15 at 09:07

1 Answers1

1

If you have a vector x, that contains numbers, you can calculate the difference by using the diff function.

In your case it would be

LifeTab1$DeathInt <- c(-diff(Sample), NA)
David
  • 9,216
  • 4
  • 45
  • 78
  • Brilliant! It worked beautifully, thanks for that, as well as the for(loop) advice as well. Why is there an 'NA' after '-diff(Sample)' though? – Lalochezia Nov 06 '15 at 09:14
  • `diff` gives you the difference, however `length(x) == length(diff(x))` returns `FALSE` as `diff(x)` is one shorter. I.e., we don't know the last difference, which is left empty by the function and we need to specifiy by hand that it is missing. If the answer was usefull, please consider marking it as the solution! – David Nov 06 '15 at 09:18
  • It was! But I don't know how to set it as a solution though >___ – Lalochezia Nov 06 '15 at 09:40
  • On the left of the answer is an up-arrow, a 0 and a down-arrow, below the down-arrow should be a tick-mark! Also, have a look at this: http://stackoverflow.com/tour – David Nov 06 '15 at 09:50