Commit e75cc48e authored by Virgile Baudrot's avatar Virgile Baudrot
Browse files

Initial commit

parents
#' @title find index
#'
#' @param x a data.frame
#' @param key character string. name of the column to select
#' @param value value of the element to return index from the column defined by key
#'
#' @return vector if not all index are equal. scalar if all equal.
#'
#' @export
#'
fd_match <- function(x, key, value){
returnID = sapply(1:nrow(x), function(i){ match(value, x[[key]][[i]]) })
if(length(unique(returnID)) == 1){ returnID = unique(returnID) }
return(returnID)
}
#' @title Convert list.column data.frame into scalar.column data.frame
#'
#' @description Convert data.frame with 2 column.list into data.frame with only column.scalar
#'
#' @name fd_melt
#'
#' @param x A data.frame
#' @param key1 character string. The name of the column to select
#' @param key2 icharacter string. The name of the column to select
#' @param id name of the replicate for the id. As to be of the same length as the number of row of the x object
#'
#' @export
#'
fd_melt_DOUBLE <- function(x, key1, key2, id = NULL){
lgth_key1 = sapply(x[[key1]], length)
lgth_key2 = sapply(x[[key2]], length)
if(any(lgth_key1 != lgth_key2)){ stop("length of element from 'key1' and 'key2' differ.")}
if(is.null(id)) {
id = 1:length(lgth_key1)
} else{
id = x[[id]]
}
df = data.frame(
id = do.call("c", lapply(1:length(id), function(i) rep(id[i], lgth_key1[[i]])))
)
df[[key1]] = do.call("c", x[[key1]])
df[[key2]] = do.call("c", x[[key2]])
return(df)
}
#' @name fd_melt
#'
#'
#' @export
#'
fd_melt_SINGLE <- function(x, key1, id = NULL){
lgth_key1 = sapply(x[[key1]], length)
if(is.null(id)) {
id = 1:length(lgth_key1)
} else{
id = x[[id]]
}
df = data.frame(
id = do.call("c", lapply(1:length(id), function(i) rep(id[i], lgth_key1[[i]])))
)
df[[key1]] = do.call("c", x[[key1]])
return(df)
}
#' @name fd_melt
#'
#' @param keep vector of column name to keep
#'
#' @export
#'
fd_melt <- function(x, key1, key2 = NULL, id = NULL, keep = NULL){
if(is.null(key2)){
DF = fd_melt_SINGLE(x, key1, id)
} else{
DF = fd_melt_DOUBLE(x, key1, key2, id)
}
# --- keep
if(!is.null(keep)) {
for(i in 1:length(keep)){
DF[[keep[[i]]]] = fd_melt_STICK(x, key1, keep[[i]])
}
}
return(DF)
}
#' @name fd_melt
#'
#' @export
#'
fd_melt_STICK = function(x, key1, keep){
vec = do.call("c", lapply(1:nrow(x),
function(i){ rep(x[[keep]][i], length(x[[key1]][[i]]))}))
return(vec)
}
#' @title Combine list of data.frame by Rows
#'
#' @description Function used to filter functional data in data.frame.
#'
#' @return Return a data.frame
#'
#' @param ls A list of data.frame
#' @param id id to provide to each data.frame. Must be the length of the list
#'
#' @export
#'
fd_rbindLStoDF <- function(ls, id = NULL){
df <- do.call("rbind", ls)
nrow_df <- sapply(ls, nrow)
if(is.null(id)) { id = 1:length(nrow_df) }
df[["id"]] = do.call("c", lapply(1:length(nrow_df), function(i) rep(id[i], nrow_df[[i]])))
return(df)
}
Version: 1.0
RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX
AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace,vignette
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/add.R
\name{fd_add}
\alias{fd_add}
\alias{fd_add_NAME}
\alias{fd_add2}
\title{Add functional data}
\usage{
fd_add(x, key, FUN, ...)
fd_add_NAME(x, key, FUN, ...)
fd_add2(x, keyConstraint, key, FUN, ...)
}
\arguments{
\item{x}{object of class data.frame.}
\item{key}{the name of the new column, as strings or symbols}
\item{FUN}{the function to be applied to each element of \code{x}.
In the case of functions like +, %*%, the function name must be
backquoted or quoted. See lapply functions for details.}
\item{\dots}{optional arguments to FUN. See lapply functions for details.}
\item{keyConstraint}{character string. The reference of the column to be checked}
}
\value{
An object with addition functional data feature (or feature dynamic).
}
\description{
Add functional data in data.frame object.
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/filter.R
\name{fd_filter}
\alias{fd_filter}
\alias{fd_filter1}
\alias{fd_filter_}
\alias{fd_filter2}
\alias{fd_filter3}
\title{Function used to filter functional data in data.frame object.}
\usage{
fd_filter1(x, key, index)
fd_filter_(x, key, index)
fd_filter2(x, key1, key2, index)
fd_filter3(x, key1, key2, key3, index)
fd_filter(x, keys, index)
}
\arguments{
\item{x}{a data.frame object.}
\item{key}{character string. The name of the column to select.}
\item{index}{integer (or vector). The index of the functional data.}
\item{key1}{character string. The name of the column to select}
\item{key2}{character string. The name of the column to select}
\item{key3}{character string. The name of the column to select}
\item{keys}{list of names of the column to select}
}
\description{
Function used to filter functional data in data.frame object.
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/match.R
\name{fd_match}
\alias{fd_match}
\title{find index}
\usage{
fd_match(x, key, value)
}
\arguments{
\item{x}{a data.frame}
\item{key}{character string. name of the column to select}
\item{value}{value of the element to return index from the column defined by key}
}
\value{
vector if not all index are equal. scalar if all equal.
}
\description{
find index
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/melt.R
\name{fd_melt}
\alias{fd_melt}
\alias{fd_melt_DOUBLE}
\alias{fd_melt_SINGLE}
\alias{fd_melt_STICK}
\title{Convert list.column data.frame into scalar.column data.frame}
\usage{
fd_melt_DOUBLE(x, key1, key2, id = NULL)
fd_melt_SINGLE(x, key1, id = NULL)
fd_melt(x, key1, key2 = NULL, id = NULL, keep = NULL)
fd_melt_STICK(x, key1, keep)
}
\arguments{
\item{x}{A data.frame}
\item{key1}{character string. The name of the column to select}
\item{key2}{icharacter string. The name of the column to select}
\item{id}{name of the replicate for the id. As to be of the same length as the number of row of the x object}
\item{keep}{vector of column name to keep}
}
\description{
Convert data.frame with 2 column.list into data.frame with only column.scalar
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/melt.R
\name{fd_rbindLStoDF}
\alias{fd_rbindLStoDF}
\title{Combine list of data.frame by Rows}
\usage{
fd_rbindLStoDF(ls, id = NULL)
}
\arguments{
\item{ls}{A list of data.frame}
\item{id}{id to provide to each data.frame. Must be the length of the list}
}
\value{
Return a data.frame
}
\description{
Function used to filter functional data in data.frame.
}
---
title: "start"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{start}
%\VignetteEncoding{UTF-8}
%\VignetteEngine{knitr::rmarkdown}
editor_options:
chunk_output_type: console
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
The package `fdf` allow to work with column-list. When for instance you have a list of time series that you want to include in a `data.frame`.
Since it works only with `data.frame` object, there is no new object, you can use very easily with any package like `dplyr` (and the tidyverse) and spatio-temporal object with package `sf` for instance.
```{r setup}
library(fdf)
```
# Add a new column-list with `fd_add`
```{r addData}
DF = data.frame( A = 1:10)
DFD = fd_add(DF, "B", function(i) rnorm(runif(1, 4,20)))
DFD
```
# Filter element in a column-list with `fd_filter`
```{r filter}
fd_filter(DFD, "B", 1)
fd_filter(DFD, "B", 2)
fd_filter(DFD, "B", 3)
```
# Find index of element in column-list matching with a key using `fd_match`
```{r}
DFD = fd_add(DFD, "C", function(i) sample(1:5, 5))
idMatch = fd_match(DFD, "C", 4)
idMatch
# to check this:
fd_filter(DFD, "C", idMatch)
```
# Melting column-list to a classical data.frame with `fd_melt`
```{r meltSimple}
fd_melt(DFD, "B")
fd_melt(DFD, "B", keep = "A")
fd_melt(DFD, "B", keep = list("A", "C"))
```
when two column list have the same length for each elements, we can melt in the same time
```{r meltDouble}
DFD = fd_add(DFD, "B2", function(i) 1:length(DFD$B[[i]]))
fd_melt(DFD, "B", "B2")
fd_melt(DFD, "B", "B2", keep = list("A", "C"))
```
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment