as_shape_col_default: Convert a 'data.frame' or 'list' to a 'shape_col_default'

as_shape_col_defaultR Documentation

Convert a data.frame or list to a shape_col_default

Description

shape_col_default() constructs a data.frame of class shape_dat with one list column containing shapes as a list of data.frames (or objects convertible to such).

Usage

as_shape_col_default(x, ...)

## Default S3 method:
as_shape_col_default(x, formula, validate = TRUE)

as_shape_col(x, ...)

## S3 method for class 'shape_frame'
as_shape_col(x, shape.name = NULL, ...)

## S3 method for class 'shape_dat'
as_shape_col_default(x, ...)

Arguments

x

optional data frame or list with relevant data.

formula

formula stating identifying 'shape' variable and its internal strucure in the format shape ~ cbind(value1, value2) ~ arg.

validate

logical, should an additional validation of the constructed shape_dat be performed (if any)? Defaults to TRUE. FALSE might speed up computation time.

Examples

library(manifoldboost)
library(cubelyr)
# generate toy shapes
arg <- 1:20 / 20
id <- 1:8
n_arg <- length(arg)
n_id <- length(id)
x <- (rep(id,each=n_arg)/5 + 1) * cos(arg * 2*pi) + 2*cos(arg * 4*pi)
y <- (rep(id,each=n_arg)/5 + 1) * sin(arg * 2*pi) + 2*sin(arg * 4*pi)

# set up data in very long format
dat_longlong <- data.frame(
  wert = c(x,y), 
  d = rep(c("x","y"), each = n_arg*n_id),
  time = rep(arg, 2*length(id)), 
  ID = rep(rep(id, each = n_arg), 2)
  )

dat_longlong <- as_shape_dat(dat_longlong, wert^d ~ time | ID)

dat_default_ <- as_shape_frame_default(dat_longlong, 
                                       value.dim.names = NULL)

# ... and in the default format
dat_long <- data.frame(
  x = x, 
  y = y, 
  arg = rep(arg, n_id),
  id = rep(id, each = n_arg)
)
dat_long <- as_shape_dat(dat_long, cbind(x,y) ~ arg | id)

dat_longlong_ <- as_shape_frame_long(dat_long)
dat_long_ <- as_shape_frame_default(dat_longlong)

# ... and convert it to a shape_col
dat_long_col <- as_shape_col(dat_long_)
formula(dat_long_col)
shape_names(dat_long_col)

# ... and shape_col back to dat_long
dat_long_ <- as_shape_frame(dat_long_col)
formula(dat_long_)
class(dat_long_)

# ... also as shape_cube (as it is regular)
shp <- as.tbl_cube(dat_longlong, met_name = "wert")
dat_cube <- as_shape_dat(formula = shp ~ wert^d ~ time | ID)
dat_cube_ <- as_shape_cube(dat_longlong)

# ... or at last as shape_list
shp_list <- split(dat_longlong[, -4], dat_longlong$ID)
dat_list <- as_shape_dat(formula = shp_list ~ wert^d ~ time)

# plot long shapes per dimension:id
opar <- par(mfrow = c(2, 2))
lapply( split( shp, interaction(shp$.dim, shp$.id), drop = FALSE )[1:4], function(x) 
  with(x, plot(.arg, .value, t = "b", main = paste("id", .id[1], "dim", .dim[1]))) )  
par(opar)

# convert to array in the format of R package shapes
ashp <- as.array(shp)
plot(x = 0, y = 0, col = "white", xlim = range(ashp[,1,]), ylim = range(ashp[,2,]))
apply(ashp, 3, lines, t = "l")

# .. and back, e.g., as wide shapes 
opar <- par(mfrow = c(3, 3))
lapply( split( as_shape_wide(ashp), as_shape_wide(ashp)$.id ), function(x) 
  with(x, plot(.value1, .value2, t = "b")) )  
par(opar)

Almond-S/manifoldboost documentation built on June 23, 2022, 11:06 a.m.