Description Usage Arguments Value Author(s) Examples
Often times, an individual has multiple observations in the dataset. last.sample
will
loop through the entire dataset and return only one observation per individual, giving the
first (or last) draw for a person, or performing some function on the variable of interest.
1 2 3 4 5 6 7 8 9 |
ID |
The name of the unique identifier, expressed as a string (e.g., "Match.Group") |
sort.var |
The variable to be sorted on in order to take the first (or last) sample, expressed as a string. |
decreasing |
How should the sort.var variable be sorted? Defaults to T. |
data |
The dataset with multiple rows per individual |
FUN |
What should be done with the multiple samples? This function can be used to extract the last (or first) sample using the decreasing/sort.var options, or a function can be performed (such as the mean) on one or more columns. See examples. |
fun.var |
If FUN if not null, the variable (or a vector of variables), expressed as strings to have the function applied to. |
... |
Other arguments passed to the chosen function. |
a new dataframe containing one row per ID
Dustin Fife
1 2 3 4 5 6 7 8 9 10 11 | #### take only group 2 values
last.sample(ID="ID", sort.var="group", data=sleep)
#### take only group 1 values
last.sample(ID="ID", sort.var="group", decreasing=FALSE,data=sleep)
#### average group 1 and 2 values
last.sample(ID="ID", data=sleep, FUN=mean, fun.var="extra")
#### take the maximum extra value
last.sample(ID="ID", data=sleep, FUN=max, fun.var="extra")
#### take the mean of two columns extra value
sleep$group = as.numeric(as.character(sleep$group))
last.sample(ID="ID", data=sleep, FUN=mean, fun.var=c("group","extra"))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.