Learn R Programming

Morpho (version 2.1)

name2factor: extract data from array names

Description

extract data from array names

Usage

name2factor(x, sep = "_", which, collapse = sep)

name2num(x, sep = "_", which, collapse = sep, dif = TRUE)

Arguments

x
data, can be a three-dimensional array, a matrix, a named list or a vector containing names to split
sep
character by which to split the strings
which
integer or vector of integers, if more entries are selected, they will be concatenated by the string specified with the option 'collapse'.
collapse
character by which to collapse data if two strings are to be concatenated
dif
logical: calculate difference if two fields containing numbers are selected.

Value

  • returns a vector containing factors or numbers

Details

extract data from array names and convert to factors or numbers

If an array is used as input, the data info is expected to be in the 3rd dimension, for a matrix, rownames are used.

Examples

Run this code
data <- matrix(rnorm(200),100,2)
id <- paste("id",1:100,sep="")
pop <- c(rep("pop1",50),rep("pop2",50))
sex <- c(rep("male",50),rep("female",50))
age <- floor(rnorm(100,mean=50,sd=10))
rownames(data) <- paste(id,pop,sex,age,sep="_")
infos <- data.frame(pop=name2factor(data,which=2))
infos$age <- name2num(data,which=4)
infos$pop.sex <- name2factor(data,which=2:3)

Run the code above in your browser using DataLab