Archive for July, 2015

Jul
31

trunc() and floor(), round() and signif() in R

floor takes a single numeric argument x and returns a numeric vector containing the largest integers not greater than the corresponding elements of x.

trunc takes a single numeric argument x and returns a numeric vector containing the integers formed by truncating the values in x toward 0.

round rounds the values in its first argument to the specified number of decimal places (default 0).

signif rounds the values in its first argument to the specified number of significant digits.

test:

————————–

> x <- c(-5.2, -3.8, 5.2, 3.8)

>floor(x)

-6 -4 5 3

>trunc(x)

-5 -3 5 3

> x <- 3.1415

—————————-

>round(x)

3

>round(x,3)

3.142

>signif(x,3)

3.14

Jul
25

Plot Histogram in R

Plot Histogram in R:

Method 1: hist(vector, breaks)

Take care for the parameter “breaks”:

according the help:

breaks: a single number giving the number of cells for the histogram.

Important: the number is a suggestion only! The breakpoints will be set to pretty values.

For example:

>data(“women”) #load dateset “women”

> hist(women$weight, breaks = 7)

histogram

 

 

 

 

 

 

If you want to have exact 7 bins, you should specify the position

>hist(women$weight, breaks=seq(min(women$weight), max(women$weight), l = 7))

histogram_exact_bins

 

 

 

 

 

 

 

so, you will get the exact 7 bins