sobota, 2 stycznia 2016

R: Just discovered Swirl, interactive courses in R console.

To install: Choose a course name from https://github.com/swirldev/swirl_courses and paste it in R command, eg:
> install_from_swirl("Regression_Models", dev = FALSE, mirror = "github")

This night I completed R programming course, an intro.

Some of my unordered notes, useful perhaps only to myself:

Working directory:
list.files() or dir()
> args(list.files)
> dir.create("testdir")
> file.create("mytest.R")
> file.exists("mytest.R")
> file.rename("mytest.R","mytest2.R")
> file.copy("mytest2.R","mytest3.R")
> file.path("mytest3.R") - good to construct relative, independent of system, paths in a procedure.
> file.path("folder1","folder2")
> dir.create(file.path("testdir2","testdir3"),recursive = TRUE) - creating dir and subdir in one shot
> unlink("testdir2", recursive=TRUE) - deleting directory with all its content, note use Recursive
> unlink("testdir", recursive=TRUE)



Sequences of numbers along some vector:
> seq(along.with = my_seq)
> seq_along(my_seq)
> rep(c(0,1,2),times = 40)
> rep(c(0,1,2), each = 10)
> file.info("mytest.R")$mode --- to grab specific info


Vectors
> paste(my_char, collapse = " ")  - collapsing string vector to one string.
> paste("Hello", "world!", sep=" ")

> paste(1:3,c("X","Y","Z"), sep="")
[1] "1X" "2Y" "3Z"

> paste(LETTERS, 1:4, sep="-")
 [1] "A-1" "B-2" "C-3" "D-4" "E-1" "F-2" "G-3" "H-4" "I-1" "J-2" "K-3" "L-4" "M-1" "N-2" "O-3" "P-4" "Q-1" "R-2" "S-3" "T-4" "U-1" "V-2" "W-3" "X-4" "Y-1"
[26] "Z-2"


indexing
> x[-c(2,10)] exclude these numbers, use - once before concatenate

==data.frame==
> colnames(my_data)<-cnames - giving names to columns


> which(ints>7) Which are less then 7?
[1] 1 3 5
> any(ints<0) are there any lesser than 7
[1] FALSE
> all(ints>0)
[1] TRUE

functions

ellipsis arguments...

mad_libs <- function(...){
  # Do your argument unpacking here!

  args <- list(...)


  place <- args[["place"]]
  adjective <- args[["adjective"]]
  noun <- args[["noun"]]

  paste("News from", place, "today where", adjective, "students took to the streets in protest of the new", noun, "being installed on campus.")
}

your own operators, eg. add texts to each other.

"%p%" <- function(left,right){
  paste(left,right)
 
}

> lapply(unique_vals, function(elem) elem[2])  - one liner function

Table apply?

> table(flags$animate) - count flags which are animate :-)

> tapply(flags$animate, flags$landmass, mean)  - get the proportion of animate elements in each landmass.

>  tapply(flags$population, flags$red, summary) - get the population summary for red in the flag.

>  tapply(flags$population, flags$landmass, summary) - get the population summary for each landmass


Aquaint with data
> summary(plants)

if categorical data gets truncated and I need to analyse it:
> table (plants$Active_Growth_Period)

But the most concise info on data one gets via
> str(plants)

Simulation:
Dice throwing 4 times:
> sample(1:6,4, replace = TRUE)
Unfair coin flips:
> flips<- sample(c(0,1),100,replace=TRUE, prob=c(0.3,0.7))
> rbinom(1,size=100, prob=0.7) - number of heads
> rbinom(100, size=1, prob=0.7) - set of all observations


 Each probability distribution in R has an
r*** function (for "random"), a d*** function (for "density"), a p*** (for "probability"), and q*** (for
| "quantile").


Time:
> str(unclass(t2)) to have a more compact view of all components of a structure.
> t4<- strptime(t3,  "%B %d, %Y %H:%M") - check it out, it converts txt dates into POSIXlt
> difftime(Sys.time(),t1, units='days') time difference in days



Plot:
> plot(cars, xlim = c(10,15)) - limit x to 10..15 range
> plot(cars, pch = 2) - change tick symbols (here to triangles)

> boxplot(mpg ~ cyl, data = mtcars)   y mpg to x cyl from data = mtcars
> hist(mtcars$mpg)


Brak komentarzy:

Prześlij komentarz