diff -r a260027dfa5b -r 998d6c646f20 r.rst --- a/r.rst Sat Mar 05 22:43:02 2016 +0200 +++ b/r.rst Sat Mar 05 22:50:41 2016 +0200 @@ -38,6 +38,13 @@ Objext size in memory: ``object.site(1:2)`` +Interactive session +=================== + +Controlling output precision:: + + options(digits=3) + Debugging ========= @@ -108,11 +115,50 @@ Looping over data ================= -``lapply`` iterate over data and return list of function application:: +``lapply`` iterate over data and return list with result of function +application:: lapply(1:5, function(x) x^2) lapply(matrix(rnorm(20*10),20,10), mean) +Usually you don't need a list but a vector. ``sapply`` works like ``lapply`` but +also try to convert result to matrix or vector is dimantions and elvement types +permit this:: + + lapply(list(1:5), mean) + [[1]] + [1] 3 + sapply(list(1:5), mean) + [1] 3 + +``apply`` works on specific dimension of data so useful to work with matrixes +and data frames:: + + apply(matrix(1:6, 2, 3), 1, min) + [1] 1 2 + + apply(matrix(1:6, 2, 3), 2, max) + [1] 2 4 6 + + apply(array(rnorm(2*2*10), c(2, 2, 10)), c(1, 2), mean) + [,1] [,2] + [1,] -0.2733804 0.3154234 + [2,] 0.1830982 -0.5889010 + +``colSums``, ``rowSums``, ``colMeans``, ``rowMeans`` is defined as optimized +equivalent for:: + + rowSums = apply(x, 1, sum) + colSums = apply(x, 2, sum) + rowMeans = apply(x, 1, mean) + colMeans = apply(x, 2, mean) + +``split`` partitioning data on factor (analog of SQL ``group by``):: + + data<-data.frame(rnorm(10),rbinom(10,1,prob=.7)) + sdata<-split(data[,1],data[,2]) + lapply(sdata,mean) + Exploring data ============== @@ -148,3 +194,10 @@ hist(rpois(100,10)) hist(rpois(100,10),breaks=20) +Renaming columns +================ +:: + + names(d)[names(d)=="beta"] <- "two" + names(d)[2] <- "two" +