Chandra's tips and Cheat Sheat for R

Below is code for adding extra columns to a dataframe, based on condtion of anothe column. I converted a quantitative varibale 'Area' in data frame olive to categorical variable using a condition that, if Area =1, then V10 = a and so on

olive$V10 <- cut(olive$Area, c(-Inf, 1, 2, 3, 4, 5, 6, 7, 8, 9), labels = c("a", 
    "b", "c", "d", "e", "f", "g", "h", "i"), right = TRUE)
## Error: object 'olive' not found

If olive$area needs to be considered as factor variable, then

# olive$Area <- as.factor(olive$Area).

Above is simple. But, if one wants to anothe column to data frame, then another solution is:

mysize <- function(x) {
    if (x < 500) 
        return(1)
    if (500 <= x & x < 1000) 
        return(2)
    if (1000 <= x & x < 2000) 
        return(3)
    if (2000 <= x & x < 3000) 
        return(4)
    if (3000 <= x & x <= 5000) 
        return(5) else return(NA)
}
# table$population.bin <- sapply(table$population, mysize) table

Below are relevant stackexchange links

#
# http://stackoverflow.com/questions/13540080/adding-a-new-column-in-r-data-frame-with-values-conditional-on-values-of-another
# http://stackoverflow.com/questions/5824173/replace-a-value-in-a-data-frame-based-on-a-conditional-if-statement-in-r