Learn R Programming

datana (version 1.1.4)

assigncl: Function to assign classes based upon a variable of interest.

Description

Assigns class of each observation in a dataframe

Usage

assigncl(
  data = data,
  variable = variable,
  num.class = 4,
  breaks = NULL,
  wclass = NULL,
  mincl = NULL,
  name.class = NULL
)

Value

The main output is the data including a new column having the created class variable.

Arguments

data

a dataframe having the variable of interest for each observation.

variable

a character giving the column name of the numeric variable to be used for defining the limits of each class.

num.class

the number of classes to be build. The default is set to 4. Regardless, the percentiles are used to set the limits of each class.

breaks

is a vector having the numbers to be used as breakpoints, by default is set to NULL, therefore the breakpoints will be determined by the num.class.

wclass

a number defining the width or amplitud of the classes. By default is set to NULL, otherwise, the width is determined by the previous explained options, such as, breaks or num.clases.

mincl

the number of the minimum class to be used. By default is set to NULL, otherwise, this option is used to define the breaks.

name.class

a character giving the column name of the new class variable. By default is set to NULL, then, the column name will be a composite-name merging the character provided in variable followed by ".class". Otherwise, will be name.class.

Author

Christian Salas-Eljatib and Marcos Marivil.

Details

The function assign a class or category to a random variable of interest. Several alternatives are implemented to define the way on which the allocation to a respective class is carried out.

References

  • Salas C. 2002. Ajuste y validación de ecuaciones de volumen para un relicto del bosque de roble-laurel-lingue. Bosque 23(2):81–92. tools:::Rd_expr_doi("10.4067/S0717-92002002000200009").

Examples

Run this code

# The data  
library(datana)
maple
# Example 1
graphics::boxplot(maple$dbh)
df<-assigncl(data=maple,variable="dbh")
head(df)
table(df$dbh.class)
# Example 2, changing the number of classes
df<-assigncl(data=maple,variable="dbh",num.class=5)
table(df$dbh.class)
tapply(df$dbh,df$dbh.class,range)
# Example 3, fixing the breakpoints
df<-assigncl(data=maple,variable="dbh",
             breaks = c(25.60,36.44,40.12,42.3))
table(df$dbh.class)
tapply(df$dbh,df$dbh.class,range)
# Example 4, giving the amplitude
# of the classes
df<-assigncl(data=llancahue,variable="dbh",wclass = 5)
table(df$dbh.class)
tapply(df$dbh,df$dbh.class,range)

Run the code above in your browser using DataLab