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
A[-c(1,3),] What does this means ?
The use of a negative sign - in the index tells R to keep all rows or columns except those indicated in the index.
dim (A)?
The dim() function outputs the number of rows followed by the number of columns of a given matrix.
read.table() ?
primary way to import data
write.table() ?
We can use the function write.table() to export data.
What is data frame?
R store objects in a form called Data Frame
persp() can be use to create three dimentional plot
persp() can be used to produce a three-dimensional
plot.
Option + ‘-‘?
shorcut for input operator in r
read.csv()
easiest way to load data in R
hearder = T in read.csv why ?
Using the option header=T (or header=TRUE) in the read.table() function tells R that the first line of the file contains the variable names,
na.strings in read.csv why ?
na.strings tells R that any time it sees a particular character or set of characters (such as a question mark), it should be treated as a missing element of the data matrix
e na.omit(
Auto=na.omit(Auto)
There are various ways to deal with the missing data. In this case, only five of the rows contain missing observations, and so we choose to use the na.omit() function to simply remove these rows.
names() function what it is used?
use to print names of the variable names
Why variables use $? and what is diff $ and attach()
To refer to a variable, we must type the data set and the variable name joined with a $ symbol
Alternatively, we can use the attach() function in order to tell R to make the variables in this data frame available by name
> plot(Auto$cylinders , Auto$mpg )
attach(Auto)
plot(cylinders , mpg
how to convert quantitative variavle to qualitative variables?
The as.factor() function converts quantitative variables into qualitative variables.
cylinders=as.factor(cylinders)
The cylinders variable is stored as a numeric vector, so R has treated it as quantitative. However, since there are only a small number of possible values for cylinders, one may prefer to treat it as a qualitative variable
hist()
can be use to create histogram
e.g his(mpeg , )
pairs()
creates scatter plot matrix
summary() function ?
The summary() function produces a numerical summary of each variable in a particular data set. we can also produce summary of a single variable
exiting R how ?
type q()
savehistory()
Before exiting R, we may want to save a record of all of the commands that we typed in the most recent session; this can be accomplished using the savehistory() function
loadhistory()
Next time we enter R, we can load that history using the loadhistory() function.