long: Create a long file from a wide file

Description Usage Arguments Details Value Examples

Description

Uses a minimal number of arguments to create a long file using stats::reshape. Produces output even when long variable names and time values are not fully crossed.

Usage

1
2
long(data, sep = "_", timevar = "time", idvar = "id",
  ids = 1:nrow(data), expand = TRUE, safe_sep = "#%@!", ...)

Arguments

data

wide data frame

sep

(default '_') single character separator between long names and 'time' value. Variable names with this separator are transformed to long variables.

timevar

(default 'time') names of variable in output long file to identify occasions. Its values are taken from the suffix following the 'sep' character in each time-varying variable.

idvar

(default: 'id') variable name used in output long to identify row in input wide file. It may exist in the input wide file and must, in that case, have a unique value in each row. If not, its values are replaced with row numbers.

ids

(default 1:nrow(data)) values for idvar in long file if the variable idvar does not exist in in the input wide file. Ignored if idvar exists in data.

expand

(default TRUE): if 'time' values are inconsistent, fill in missing 'time's with NAs.

safe_sep

temporary safe? separator

...

additional parameters are passed to reshape.

Details

long is intended for the simple case in which 'wide' variables in the input data frame are identified by the fact that they contain a separator character that separates the name of the variable in the long file from the value of the 'time' variable that identifies the corresponding row in the long file, e.g x_1, x_2, x_3 or brain.volume_left, brain.volume_right. If the separator ('_' by default) occurs in other variables, it must be temporarily substituted.

rehape does not work if long variable names and time values are not fully crossed, e.g x_1, x_2, x_3, y_1, y_2. By default long creates additional variables with "NAs" so the set of variables given to reshape is fully crossed, e.g. adding a variable y_3 <- NA.

Value

'long' file with each wide row repeated as many times as there are distinct values for the 'timevar' variable.

Examples

1
2
3
4
5
6
7
8
z <- data.frame(id =letters[1:10], id2= 11:20, v_L = 1:10, v_R = 11:20)
long(z)
long(z, timevar = 'Side', idvar = 'idn', ids = LETTERS[1:10])
long(z, timevar = 'Side', idvar = 'idn', ids = z$id2)

# unbalanced times
z <- data.frame(id =letters[1:10], id2= 11:20, v_L = 1:10, v_R = 11:20, z_L = 21:30)
long(z)

gmonette/spidanew documentation built on May 17, 2019, 7:27 a.m.