lab_test

2819 days ago by aniag

%r zad1.1 = function() { y = c(2,3,5,7,11,13) print(y) print(length(y)) x = 2*1:5-1 print(x) print(length(x)) print(x + y) print(sum(x>5 | x<3)) print(y[-(3:5)]) print(y[x]) } zad1.1() 
       
[1]  2  3  5  7 11 13
[1] 6
[1] 1 3 5 7 9
[1] 5
[1]  3  6 10 14 20 14
[1] 3
[1]  2  3 13
[1]  2  5 11 NA NA
Warning message:
In x + y : longer object length is not a multiple of shorter object
length
%r zad1.2 = function(x=c(17,16,20,24,22,15,21,15,17,22)) { print(summary(x)) print(sd(x)) print(sum(x<(mean(x)-sd(x)))) print(sum(x>(mean(x)+sd(x)))) print(mean(x[x<quantile(x,.25)])) print(mean(x[x>quantile(x,.75)])) } zad1.2() 
       
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  15.00   16.25   18.50   18.90   21.75   24.00 
[1] 3.28126
[1] 2
[1] 1
[1] 15.33333
[1] 22.66667
%r zad1.3 = function() { data(mtcars) # zbedne jak sie okazuje - w wiekszosci przyp. odrazu wczytany # 1. mmpg = max(mtcars[,"mpg"]) mmpg.car = rownames(mtcars)[which.max(mtcars[,"mpg"])] print(paste("Max miles per galon:",mmpg.car,mmpg)) # 2. hp.nv = mtcars[,"hp"] names(hp.nv) = rownames(mtcars) top=3 print(paste("Top",top,"horse power")) ranks = rank(hp.nv, ties.method="max") print(sort(hp.nv[ranks>=(max(ranks)-top+1)], decreasing=TRUE)) # zauwazcie, ze print(sort(hp.nv, decreasing=TRUE)[1:top]) # zwraca jeden samochod za malo dla top==3 # 3. print("Mean and SD") cmsd = function(x) cat("\t",mean(x),"+/-",sd(x),"\n") cat("\tall cars\n") cmsd(hp.nv) cat("\tall with automatic\n") cmsd(hp.nv[!mtcars[,"am"]]) cat("\tall without automatic\n") # operator '!' nas wyreczyl od jawnej konwersji - tu trzeba as.logical cmsd(hp.nv[as.logical(mtcars[,"am"])]) cat("\tmerces\n") cmsd(hp.nv[substr(names(hp.nv),1,4)=="Merc"]) cat("\ttop 20%\n") print(hp.nv[hp.nv > quantile(hp.nv,.8)]) cmsd(hp.nv[hp.nv > quantile(hp.nv,.8)]) } zad1.3() 
       
[1] "Max miles per galon: Toyota Corolla 33.9"
[1] "Top 3 horse power"
 Maserati Bora Ford Pantera L     Duster 360     Camaro Z28 
           335            264            245            245 
 Maserati Bora Ford Pantera L     Duster 360 
           335            264            245 
[1] "Mean and SD"
	all cars
	 146.6875 +/- 68.56287 
	all with automatic
	 160.2632 +/- 53.9082 
	all without automatic
	 126.8462 +/- 84.06232 
	merces
	 134.7143 +/- 47.06632 
	top 20%
         Duster 360  Cadillac Fleetwood Lincoln Continental  
Chrysler Imperial          Camaro Z28 
                245                 205                 215         
230                 245 
     Ford Pantera L       Maserati Bora 
                264                 335 
	 248.4286 +/- 43.01882 
%r ?rank 
       
rank                   package:base                    R
Documentation

_S_a_m_p_l_e _R_a_n_k_s

_D_e_s_c_r_i_p_t_i_o_n:

     Returns the sample ranks of the values in a vector.  Ties
(i.e.,
     equal values) and missing values can be handled in several
ways.

_U_s_a_g_e:

     rank(x, na.last = TRUE,
          ties.method = c("average", "first", "random", "max",
"min"))
     
_A_r_g_u_m_e_n_t_s:

       x: a numeric, complex, character or logical vector.

 na.last: for controlling the treatment of 'NA's.  If 'TRUE',
missing
          values in the data are put last; if 'FALSE', they are put
          first; if 'NA', they are removed; if '"keep"' they are
kept
          with rank 'NA'.

ties.method: a character string specifying how ties are treated, see
          'Details'; can be abbreviated.

_D_e_t_a_i_l_s:

     If all components are different (and no 'NA's), the ranks are
well
     defined, with values in 'seq_len(x)'.  With some values equal
     (called 'ties'), the argument 'ties.method' determines the
result
     at the corresponding indices. The '"first"' method results in a
     permutation with increasing values at each index set of ties. 
The
     '"random"' method puts these in random order whereas the
default,
     '"average"', replaces them by their mean, and '"max"' and
'"min"'
     replaces them by their maximum and minimum respectively, the
     latter being the typical sports ranking.

     'NA' values are never considered to be equal: for 'na.last =
TRUE'
     and 'na.last = FALSE' they are given distinct ranks in the
order
     in which they occur in 'x'.

     *NB*: 'rank' is not itself generic but 'xtfrm' is, and
     'rank(xtfrm(x), ....)' will have the desired result if there is
a
     'xtfrm' method.  Otherwise, 'rank' will make use of '==',
'>' and
     'is.na' methods for classed objects, possibly rather slowly.

_V_a_l_u_e:

     A numeric vector of the same length as 'x' with names copied
from
     'x' (unless 'na.last = NA', when missing values are removed). 
The
     vector is of integer type unless 'ties.method = "average"' when
it
     is of double type (whether or not there are any ties).

_R_e_f_e_r_e_n_c_e_s:

     Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New
S
     Language_.  Wadsworth & Brooks/Cole.

_S_e_e _A_l_s_o:

     'order' and 'sort'.

_E_x_a_m_p_l_e_s:

     (r1 <- rank(x1 <- c(3, 1, 4, 15, 92)))
     x2 <- c(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5)
     names(x2) <- letters[1:11]
     (r2 <- rank(x2)) # ties are averaged
     
     ## rank() is "idempotent": rank(rank(x)) == rank(x) :
     stopifnot(rank(r1) == r1, rank(r2) == r2)
     
     ## ranks without averaging
     rank(x2, ties.method= "first")  # first occurrence wins
     rank(x2, ties.method= "random") # ties broken at random
     rank(x2, ties.method= "random") # and again
     
     ## keep ties ties, no average
     (rma <- rank(x2, ties.method= "max"))  # as used classically
     (rmi <- rank(x2, ties.method= "min"))  # as in Sports
     stopifnot(rma + rmi == round(r2 + r2))