wide2long: Convert a record table from a wide to long formatting

View source: R/wide2long.R

wide2longR Documentation

Convert a record table from a wide to long formatting

Description

If there are multiple columns in a record table that provide the same information, for example when metatags are used to provide information on multiple individuals in the same image (e.g. Ind1Species, Ind1Age, Ind2Species, Ind2Age, etc.), the user may want to collate all these in one column (e.g. Species and Age). This function facilitate this process and removes lines where all the information being collated is missing (e.g. when there are no additional individuals) if rm.allNA=TRUE [default]. See the first example for this situation.

Usage

wide2long(
  intable,
  pattern,
  variableName = "variable",
  valueNames,
  rm.allNA = TRUE
)

Arguments

pattern

Character. The pattern to identify the columns names that need to be collated (e.g. c("Species$", "Age$"))

rm.allNA

whether rows where the values for the columns valueNames are all NA should be removed

valuesNames

Character. The vector of names to apply to the combined variables

Details

There might be situation when both wide2long and oneRowOneDetection need to be used. For example, when the metatags describe groups on individuals but the desired end point is one row for each individuals. The user has to careful think in which order to use these function. See the second example for this situation.

Note that when pattern enconter mutliple coluumns names these are grouped in the same order as the are encountered. In the example below, "Ind1Species" and "Ind1Sex" are keep together because these are encountered in this order, and before "Ind2Species" and "Ind2Sex". data.table::setcolorder can be used to set the order of the columns if needs be [see ?data.table::setcolorder].

Value

A record table

Examples

# make up a recordTable where multiple individuals in the same image
# are identified with the prefix Ind1 or Ind2
recTest <- data.table(fileName=LETTERS[1:5],
Count=c(rep(2,4), 1),
Ind1Species= c(rep("sambar", 4), "Fallow"),
Ind1Sex=sample(c("Male", "Female", NA), 5, replace = T),
Ind2Species= c(rep("sambar", 4), NA),
Ind2Sex=c(sample(c("M", "F", NA), 4, replace = T), NA) )
recTest
wide2long(recTest, pattern = c("Species$", "Sex$"), variableName="Individual",
valueNames = c("Species", "Sex"))

# In this second examples metadata refer to groups of individuals and need to
#  be collated and then
# repeated to have one entry for each detection

# Make up a test recTable
recTest <- data.table(fileName=LETTERS[1:5],
Group1Count=c(rep(2,4), 1), # The count of individuals in the first group (G1)
Group2Count=c(rep(1,4), NA), # The count of individuals in the second group (G2)
Group1Species= c(rep("sambar", 4), "Fallow"), # The species of G1
Group1Sex=sample(c("Male", "Female", NA), 5, replace = T), # The sex of G1
Group2Species= c(rep("sambar", 4), NA), # The species of G2
Group2Sex=c(sample(c("M", "F", NA), 4, replace = T), NA) # The sex of G2
)
recTest

# Apply wide2 long first
W2L <- wide2long(recTest, pattern = c("Count$", "Species$", "Sex$"),
valueNames = c("Count", "Species", "Sex"))
# Replicate rows
OROD <- oneRowOneDetection(W2L, countsName = "Count")
OROD


carlopacioni/camtrapRdeluxe documentation built on Nov. 29, 2023, 3:37 a.m.