as.foldert.data.frame: Data frame to foldert

View source: R/as.foldert.data.frame.r

as.foldert.data.frameR Documentation

Data frame to foldert

Description

Builds an object of class foldert from a data frame.

Usage

## S3 method for class 'data.frame'
as.foldert(x, method = 1, ind = 1, timecol = 2, nvar = NULL, same.rows = TRUE, ...)

Arguments

x

data frame.

method

1 or 2. Indicates the layout of the data frame x and, therefore, the method used to extract the data and build the foldert.

  • If method = 1, there is a column containing the identifiers of the measured objects and a column containing the times. The other columns contain the observations.

  • If method = 2, there is a column containing the identifiers of the measured objects, and the observations are organized as follows:

    • the observations corresponding to the 1st time are on columns timecol : (timecol + nvar - 1)

    • the observations corresponding to the 2nd time are on columns (timecol + nvar) : (timecol + 2 * nvar - 1)

    • and so on.

ind

string or numeric. The name of the column of x containing the indentifiers of the measured objects, or the number of this column.

timecol

string or numeric.

  • If method = 1, timecol is the name or the number of the column of x containing the times of observation, or the number of this column. x[, timecol] must be of class "numeric", "ordered", "Date", "POSIXlt" or "POSIXct", otherwise, there is an error.

  • If method=2, timecol is the name or the number of the first column corresponding to the first observation. If there are duplicated column names and several columns are named by timecol, the first one is considered.

nvar

integer. If method=2, indicates the number of variables observed at each time.

Omitted if method=1.

same.rows

logical. If TRUE (default), the elements of the returned foldert are data frames with the same row names.

Necessarily TRUE if method = 2.

...

further arguments passed to or from other methods.

Value

an object ft of class foldert, that is a list of data frames organised according to time; these data frames have the same column names.

If method = 1, they can have the same row names (attr(ft, "same.rows") = TRUE) or not (attr(ft, "same.rows") = FALSE). The time attribute attr(ft, "times") has the same class as x[, timecol] (numeric vector, ordered factor or object of class "Date", "POSIXlt" or "POSIXct") and contains the values of x[, timecol].

If method = 2, they necessarily have the same row names: attr(ft, "same.rows") = TRUE and attr(ft, "times") is 1:length(ft).

The rownames of each data frame are the identifiers of the individuals, as given by x[, ind].

Author(s)

Rachid Boumaza, Pierre Santagostini, Smail Yousfi, Gilles Hunault, Sabine Demotes-Mainard

See Also

foldert: objects of class foldert.

as.data.frame.foldert: build a data frame from an object of class foldert.

as.foldert.array: build an object of class foldert from a 3d-array.

Examples

# First example: method = 1

times <- as.Date(c("2017-03-01", "2017-04-01", "2017-05-01"))
x1 <- data.frame(t=times[1], ind=1:6,
                 f=c("a","a","a","b","b","b"), z1=rep(0,6), z2=rep(0,6),
                 stringsAsFactors = TRUE)
x2 <- data.frame(t=times[2], ind=c(1,4,6),
                 f=c("a","b","b"), z1=rnorm(3,1,1), z2=rnorm(3,3,2),
                 stringsAsFactors = TRUE)
x3 <- data.frame(t=times[3], ind=c(1,3:6),
                 f=c("a","a","a","b","b"), z1=rnorm(5,3,2), z2=rnorm(5,6,3),
                 stringsAsFactors = TRUE)
x <- rbind(x1, x2, x3)

ft1 <- as.foldert(x, method = 1, ind = "ind", timecol = "t", same.rows = TRUE)
print(ft1)

ft2 <- as.foldert(x, method = 1, ind = "ind", timecol = "t", same.rows = FALSE)
print(ft2)

data(castles.dated)
periods <- castles.dated$periods
stones <- castles.dated$stones
stones$stone <- rownames(stones)

castledf <- merge(periods, stones, by = "castle")
castledf$period <- as.numeric(castledf$period)
castledf$stone <- as.factor(paste(as.character(castledf$castle),
                            as.character(castledf$stone), sep = "_"))

castfoldt1 <- as.foldert(castledf, method = 1, ind = "stone", timecol = "period",
                         same.rows = FALSE)
summary(castfoldt1)


# Second example: method = 2

times <- as.Date(c("2017-03-01", "2017-04-01", "2017-05-01"))
y1 <- data.frame(z1=rep(0,6), z2=rep(0,6))
y2 <- data.frame(z1=rnorm(6,1,1), z2=rnorm(6,3,2))
y3 <- data.frame(z1=rnorm(6,3,2), z2=rnorm(6,6,3))
y <- cbind(ind = 1:6, y1, y2, y3)

ft3 <- as.foldert(y, method = 2, ind = "ind", timecol = 2, nvar = 2)
print(ft3)

dad documentation built on Aug. 30, 2023, 5:06 p.m.