R - Lab 1 Flashcards
text
Makes a comment.
Use comments frequently
3+5
Addition
8
2*3
Multiplication
6
4^2
Exponents
16
exp(1.5)
Calculates e to the 1.5 power
x=exp(1.5)
Makes variable ‘x’ equal to exp(1.5)
Doesn’t report anything
Recalling a variable
Type the variable
Ex: If x=exp(1.5)
x
4.481689
log(x)
Natural log of x
What happens if you write incomplete code?
Example:
x=exp(1.5
R will give a ‘+’ on the next line
- Either finish (type ‘)’ ) or press escape
How to scroll through history of commands?
Up and down arrows
How to bring up the help file for a command?
help([command])
So:
help(exp)
How to search for functions that include a certain word?
??’[text]’
??’exponentiate’
What letter must you put in front of parenthesis to create a vector (list of values)?
c
Example vector
x=c(4,5,6)
In vectors:
x[2]
Tells us the 2nd value of x.
If
x=c(4,5,6)
Then
x[2]= 5
What will exp(x) do if x contains a vector?
Calculate exp to the power of each value in x; return a list of scalars.
What is a function?
Any command that takes arguments (in parentheses) and returns results based on a calculation.
Example functions
log(x)
exp(x)
What is a matrix?
A block of numbers with multiple rows and columns.
How to create a matrix in R?
- Create at least two vectors.
Ex:
year=c(1800,1850,1900,1950)
carbon=c(8,54,534,1630) - Assign the data.frame() function to a variable.
Ex:
datum=data.frame(Year=year,Carbon=carbon) - View by typing the variable
Ex:
datum shows the matrix
datum[2] shows just the second column
How do you show the data of just a column AS a column? How do you show the data of a column AS a row of text?
Column
datum[2]
Row of text
datum$Carbon
What are the three easy summaries/analyses of data?
summary(datum)
- provides mean and quartile
head(datum)
[SAYS ‘checks that data was imported properly’ but seems like python head()]
names(datum)
Gives you attributes of data or other objects
How do you make a simple plot?
plot(datum)
X-axis is the first variable in the matrix
plot(Carbon~Year) doesn’t work. Why?
‘Carbon’ and ‘Year’ don’t exist outside the datum data.frame