[cut | table | tapply| sapply| split ] |
The default is to use right intervals. To change it and to use left intervals do cut(x, c(....), left.include=T, inc=T).
We can name the labels as we want: cut(x, breaks=c(...),labels=c(".".,....,".")). You can also rename the obtained levels: levels(x3)_c(".",....,".").
The function pretty creates convenient breaks points for a categorical variable.
For example:
> x <-c(6,3,1,5,2,4,3,4,5,2)
> x1_cut(x,3)
> x1
[1] 3 2 1 3 1 2 2 2 3 1
attr(, "levels"):
[1] "0.95+ thru 2.65" "2.65+ thru 4.35" "4.35+ thru 6.05"
attr(, "class"):
[1] "category"
> x2_cut(x, c(0,4,7))
> x2
[1] 2 1 1 2 1 1 1 1 2 1
attr(, "levels"):
[1] "0+ thru 4" "4+ thru 7"
attr(, "class"):
[1] "category"
> x3_cut(x, c(0,quantile(x,(1:3)/3)))
> levels(x3)_c("group 1","group 2","group 3")
> x3
[1] 3 1 1 3 1 2 1 2 3 1
attr(, "levels"):
[1] "group 1" "group 2" "group 3"
attr(, "class"):
[1] "category"
> x4_cut(x, c(1,5,10), left.include=T, inc=T)
> x4
[1] 2 1 1 2 1 1 1 1 2 1
attr(, "levels"):
[1] "1 thru 5-" "5 thru 10-"
attr(, "class"):
[1] "category"
> x5_cut(age, breaks=c(0,2,4,7),labels=c("small","medimum","large"))
> x5
[1] 1 1 1 1 1 2
attr(, "levels"):
[1] "small" "medimum" "large"
attr(, "class"):
[1] "category"
> pretty(x)
[1] 1 2 3 4 5 6
> cut(x,pretty(x))
[1] 5 2 NA 4 1 3 2 3 4 1
attr(, "levels"):
[1] "1+ thru 2" "2+ thru 3" "3+ thru 4" "4+ thru 5" "5+ thru 6"
> sex_c("male","female","male","female","male","female","female")
> age_c(23,35,40,24,60,20,35)
> table(sex)
female male
4 3
> table(sex,age)
20 23 24 35 40 60
female 1 0 1 2 0 0
male 0 1 0 0 1 1
> sex_factor(c(1,2,1,2,1,2,2), labels=c("Female","Male"))
> table(sex,age)
20 23 24 35 40 60
Female 0 1 0 0 1 1
Male 1 0 1 2 0 0
> systol_c(118, 125, 128, 127, 110, 140, 130)
> tapply(systol, list(sex, age), mean)
20 23 24 35 40 60
Female NA 118 NA NA 128 110
Male 140 NA 127 127.5 NA NA
> split(c("Martin", "Mary", "Matt"), c("M", "F", "M"))
$F:
[1] "Mary"
$M:
[1] "Martin" "Matt"