R shortcuts Flashcards
shortcut
cmd + shft + c
To comment multiple code in R studio
u can use both for assigment?
equality and < sign
?funcname does what?
open windows with what the function does
ls() list what?
list of all of the objects
rm() ? What differ list and remove functions?
remove object that we dont want e.g rm(x,y)
how to remove all objects at once?
rm(list=ls())
?matrix ?
The help file reveals that the matrix() function takes a number of inputs, but for now we focus on the first three: the data (the entries in the matrix), the number of rows, and the number of columns. First, we create a simple matrix
x=matrix(data=c(1,2,3,4), nrow=2, ncol=2)
Note that we could just as well omit typing data=, nrow=, and ncol= in the matrix() command above: that is, we could just type
> x=matrix(c(1,2,3,4) ,2,2)
by default matrix create by colum, how to create it by rows?
R creates matrices by successively filling in columns. Alternatively, the byrow=TRUE option can be used to populate the matrix in order of the rows.
> matrix(c(1,2,3,4),2,2,byrow=TRUE)
sqrt(x)
calculate squaroot of X
rnorm()
generates a vector of random normal variables, with first argument n the sample size. Each time we call this function, we will get a different answer.
By default, rnorm() creates standard normal random variables with a mean of 0 and a standard deviation of 1. However, the mean and standard devi- ation can be altered using the mean and sd arguments, as illustrated above
x=rnorm(50)
y=x+rnorm(50,mean=50,sd=.1)
how to use set.seed() to create same random result ?
Sometimes we want our code to reproduce the exact same set of random numbers; we can use the set.seed() function to do this. The set.seed() function takes an (arbitrary) integer argument.
Plot
plot() function is the primary way to plot data in R
passing in the argument xlab will result in a label on the x-axis.
> plot(x,y)
plot(x,y,xlab=”this is the x-axis”,ylab=”this is the y-axis”,
main=”Plot of X vs Y”)
dev.off() it does what?
dev.off() indicates to R that we are done creating the plot.
seq(a,b)
creates sequence of numbers
seq(0,1,length=10) makes a sequence of 10 numbers that are equally spaced between 0 and 1
indexing A[2,3]
The first number after the open-bracket symbol [ always refers to the row, and the second number always refers to the column.
A[c(1,3),c(2,4)]
A[1:3,2:4]
A [1:2 ,]
A [ ,1:2]
The last two examples include either no index for the columns or no index for the rows. These indicate that R should include all columns or all rows, respectively. R treats a single row or column of a matrix as a vector