Hellow everybody, I have a dataframe with 468 individuals (rows) that I captured at least once during 28 visits (columns), it looks like: mortality[1:10,] X18.10.2004 X20.10.2004 X22.10.2004 X24.10.2004 X26.10.2004 X28.10.2004 X30.10.2004 X01.11.2004 X03.11.2004 X07.11.2004 1 1 0 0 0 1 1 1 0 0 0 2 1 0 0 0 0 0 0 0 0 0 3 1 1 1 0 0 0 1 0 0 1 4 1 0 0 0 0 0 0 0 0 0 5 1 1 1 1 0 0 1 1 0 0 6 1 1 1 1 0 0 0 1 0 0 7 1 0 1 0 1 0 1 1 0 0 8 1 1 1 0 1 0 1 1 1 1 9 1 0 0 1 1 0 0 0 1 0 10 1 0 1 0 1 0 0 0 0 0 so I can know how many times every individual was captured, 0= not capture, 1=capture. persistence<-apply(mortacap2,1,sum) I also want to know when was the first and the last capture for every individual, if I use: which(mortacap2[1,]==1) X18.10.2004 X26.10.2004 X28.10.2004 X30.10.2004 1 5 6 7 I can estimate manually row by row, but I dont get how to estimate the first and the last capture, to all individuals in the database at the same time. I tried d<-numeric(368) for (i in 1:368) {d[i]<-which(mortacap2[1:368,]==1} but it didnt work. Any help would be appreciated. Thanks in advance!! Roberto Munguia Steyer Departamento Biologia Evolutiva Instituto de Ecologia, A.C. Xalapa, Veracruz. MEXICO Windows XP R 2.10 [[alternative HTML version deleted]]
roberto munguia <munguiar <at> posgrado.ecologia.edu.mx> writes:> > I have a dataframe with 468 individuals (rows) that I captured at least once > during 28 visits (columns), it looks like: > > mortality[1:10,] > > > 1 1 0 0 0 1 1 > 1 0 0 0..> so I can know how many times every individual was captured, 0= not capture, > 1=capture.> I also want to know when was the first and the last capture for every > individual,This should give you a starter # create play data cap = data.frame(matrix(rbinom(120,1,0.3),nrow=10)) firstthat<-function(x) which(x)[1] # stolen from Thomas Lumley # Make your data logical; not really needed, but easier to understand cap.log = cap==1 apply(cap.log,1,firstthat) # gives first captures Dieter
Thanks to Patrick Burns, Dieter Menne and Peter Alspach for your help. Peter Alspach indicated me how to get the first and the last capture of every individual with the following code: capture <- matrix(rbinom(40, 1, 0.3), 4, 10) capture [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 0 0 0 0 0 1 1 0 1 1 [2,] 1 0 1 0 0 0 1 1 1 0 [3,] 0 0 0 0 0 0 1 0 1 0 [4,] 0 1 0 1 1 0 0 0 0 0 firstcap<-apply(capture, 1, function(x) min((1:length(x))[x==1])) [1] 6 1 7 2 lastcap<-apply(capture, 1, function(x) max((1:length(x))[x==1])) [1] 10 9 9 5 Roberto Hello everybody, I have a dataframe with 468 individuals (rows) that I captured at least once during 28 visits (columns), iso I can know how many times every individual was captured, 0= not capture, 1=capture. persistence<-apply(mortacap2,1,sum) I also want to know when was the first and the last capture for every individual, if I use: which(mortacap2[1,]==1) X18.10.2004 X26.10.2004 X28.10.2004 X30.10.2004 1 5 6 7 I can estimate manually row by row, but I dont get how to estimate the first and the last capture, to all individuals in the database at the same time. I tried d<-numeric(368) for (i in 1:368) {d[i]<-which(mortacap2[1:368,]==1} but it didnt work. Any help would be appreciated. Thanks in advance!! Roberto Munguia Steyer Departamento Biologia Evolutiva Instituto de Ecologia, A.C. Xalapa, Veracruz. MEXICO Windows XP R 2.10