R primer • What’s R • Data types • Value types

advertisement
R primer
•
•
•
•
•
•
•
•
What’s R
Data types
Value types
Function
Loops etc.
Scripts
Your data
How to get help
•
•
•
•
Total?
Min?
Max?
How many months was the amount > $40?
swiss
sw <- swiss[1:5, 1:4]
sw[,1:3]
sw[4:5,1:3]
sw[1]
sw[,1]
sw[1,]
sw[2,2]
sw[“C”,]
sw[c(1,1:2),]
## addting a column
sw[“new1”] <- 1:5
sw[,”new2”] <letters[1:5]
• what is a function?
• how to write one
– function(inputs) things_to_do_with_inputs
– e.g.
– unbiased <- function(x) sum(x^2)/(length(x)-1) –
length(x)*mean(x)^2/(length(x)-1)
– how to use: unbiased(c(1,2,3,4,3))
• Another estimator for the population
variance (based on a sample variance) is
• write this as a function
• NB: this has a better likelihood if population is normal
Another way to write a function
function(x) {
things
to
do
}
unbiased2 <- function(x) {
n <- length(x)
m <- mean(x)
ss <- sum(x^2)
(ss-n*m)/(n-1)
}
unbiased2(c(1,2,3,4,3))
Write a function that converts degrees Fahrenheit to
degrees Celcius
[℃] = ([°F] − 32) × 5⁄9
write a function for sample correlation coefficient
for loops:
for (var in vector_to_iterate) {
do
something
with
the changing var
}
using a “for” loop, find the median for each
variable in the swiss data
NB: Although this maybe advanced, you can do the above at
once by using a function apply
You can loop with
• numbers (does not have to be ordered)
• names
• any vector!
Why script?
• Because you have many repetitions
• Because you can “redo” everything from the
beginning
• Because you want to keep log of what you do
Is scripting hard?
• Not at all!!
• You have done it already
• To keep a log, you need to save it in a text file
Example
1.Create a text file in your working directory
e.g., Feb29.R, first_script.R
2.Open it and type what you want the script to
do:
x <- 1:10
print(x*3)
3.Go back to R; make sure you are in the same
directory; then:
source(“Feb29.R”)
Uses of scripts
• Save your functions
– **IMPORTANT** leave “comments”
• Just for logging purposes
Data – inputs and outputs
• c()
• scan()
• read(),read.csv(),
read.table()
• write.table()
• write.csv()
Data exercise
1.write the swiss data to a csv file
2.open the csv file in a spreadsheet
3.modify it as you want in the spreadsheet
4.save the spreadsheet as a csv
5.open it in R
6.check if the change you made is reflected
7.(if that's too easy, make changes so that you
have a missing values; how are you going to
get a mean for variable with missing data?)
How to get help
• No idea on how to go about?
– Google has the answer (usually)
• How to use a function?
– See the included help file
Download