Skip to content

Commit 7cae2f3

Browse files
committed
added options to scaleweight: either scaled along group membership of specified column index or scaled across all objects
1 parent 880f6a4 commit 7cae2f3

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

R/datplot_funs.R

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,24 @@ datsteps <- function(df, stepsize = 5) {
5151
#' Requires a dataframe as produced by datsteps(). (Meaning 6 columns in the following order: ID, group, minimum/earliest date, maximum/latest date, weight, 'DAT_Steps')
5252
#'
5353
#' @param df a dataframe as returned by datsteps
54-
#' @param var the column of said dataframe that should be used as the group variable
54+
#' @param var the index of the column of said dataframe that should be used as the group variable, OR "all" (note: all non-numeric values will result in the weight being scaled accross all objects)
5555
#'
56-
#' @return the same dataframe, with the 'weight'-values scaled along group membership
56+
#' @return the same dataframe, with scaled 'weight'-values
5757
#'
5858
#' @export scaleweight
5959

60-
scaleweight <- function(df, var) {
60+
scaleweight <- function(df, var = c("all", 2) ) {
6161
res_df <- data.frame(NULL)
62-
uvar <- unique(var)
63-
for (row in 1:length(uvar)) {
64-
wip <- df[which(var == uvar[row]),]
65-
wip$weight <- wip$weight / sum(wip$weight)
66-
res_df <- rbind(res_df, wip)
62+
if (is.numeric(var)) {
63+
uvar <- unique(df[,var])
64+
for (row in 1:length(uvar)) {
65+
wip <- df[which(df[,var] == uvar[row]),]
66+
wip$weight <- wip$weight / sum(wip$weight)
67+
res_df <- rbind(res_df, wip)
68+
}
69+
} else {
70+
df$weight <- df$weight / sum(df$weight)
71+
res_df <- df
6772
}
6873
return(res_df)
6974
}

0 commit comments

Comments
 (0)