diff options
Diffstat (limited to 'R/mkin_wide_to_long.R')
-rw-r--r-- | R/mkin_wide_to_long.R | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/R/mkin_wide_to_long.R b/R/mkin_wide_to_long.R new file mode 100644 index 0000000..6db2f33 --- /dev/null +++ b/R/mkin_wide_to_long.R @@ -0,0 +1,13 @@ +mkin_wide_to_long <- function(wide_data, time = "t")
+{
+ colnames <- names(wide_data)
+ vars <- subset(colnames, colnames != time)
+ n <- length(colnames) - 1
+ if (!(time %in% colnames)) stop("The data in wide format have to contain a variable named ", time, ".")
+ long_data <- data.frame(
+ name = rep(vars, each = length(wide_data[[time]])),
+ time = rep(wide_data[[time]], n),
+ value = unlist(wide_data[vars]),
+ row.names = NULL)
+ return(long_data)
+}
|