statistics - r last observation carried forward unless no further observations -


I have a data set that takes measurement of serial temperature for many patients. Datasets are small and have many NA values, however, patients may have died before the end of the experiment and hence the death was recorded from the end of the period of measurement period. What I want to do, is through my data-frame rows (individual patients), which is not taking the previous observation forward, nor have any other comments been recorded. A small example would be dataframe:

  df < -data.frame (H0 = c (35.4, 36.0, 36.0, 36.4), H1 = C (NA, 34.0, 33.4, NA), H2 = C (NA, 33.5, NA, 34.2), H3 = C (32.9, NA, 34.0, NA), H4 = C (NA, 33.1, NA, NA), H5 = C (33.2, NA, NA, 32.8))  

To work with the zoo and apply with it:

  df2 & lt; -apply (df, 1, na.locf)  

Although it creates a matrix and does not have dataframe, and by the end of the experiment, a patient exceeds the temperature for the patient, which I do not have to do. Patients 2 & amp; 3 should still end in H5 with N5.

transitions, na.fill 0 to fill the trailing NAS and Use na.locf to fill the remaining NAS and return. Finally, replace zero with zero:

  Library (zoo) df0 & lt; - T (N.F. Fill (T (DF), C (NA, NA, 0))) Ifelse (df0 == 0, NA, df0)  

[2] [, 1] [, 2] [, 3] [, 4]] [, 5] [, 6] [1,] 35.4 35.4 32.9 32.9 33.2 [2, ] 36.0 34.0 33.5 33.5 33.1 NA [3,] 36.0 33.4 33.4 34.0 NA [4,] 36.4 36.4 34.2 34.2 34.2 32.8

This can be optionally written as such:

  zero 2a & lt; - Function (x) ifelse (x == 0, NA, x) T (df, 1, function (x) zero 2 na (na. Locf (nfil (x, c (na, na, 0))))) )  

Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -