2019-01-30
After this lecture, you
?mean
help.search('weighted mean')
help(package = 'dplyr')
str(iris)
class(iris)
install.packages("tidyverse")
library(tidyverse)
getwd()
setwd('C://file//path')
4.1 Creating Vectors
c(2, 4, 6)
2:6
seq(2, 3, by=0.5)
rep(1:2, times=3)
x=rep(1:2, each=3)
x
4.2 Selecting Vector Elements
x
x[4]# fourth element.
x[-4] # All but the fourth.
x[2:4]# Elements two to four.
x[-(2:4)]# All elements except two to four.
x[c(1,5)] # Elements one and five.
x[x < 2] # All elements less than zero.
for (variable in sequence/vector){
Do something
}
# Example
for (i in 1:4){
j <- i + 10
print(j)
}
if (condition){
Do something
} else {
Do something different
}
# Example
if (i > 3){
print('Yes')
} else {
print('No')
}
function_name <- function(var){
Do something
return(new_variable)
}
# Example
square <- function(x){
squared <- x*x
return(squared)
}
square(2)
df <- read.csv('https://nb.vse.cz/~zouharj/econ/wage1.csv')
write.csv(df, 'file.csv')
head(df, 3)
library(readxl)
wage1 = read_csv('https://nb.vse.cz/~zouharj/econ/wage1.csv')
head(wage1,3)
# Note that the Excel spreadsheet must be local (a URL does not work).
wage2 = read_excel('wage2.xls', sheet = 1)
head(wage2,3)
x = wage1$wage[1:10]
t(x)
t(round(x, 1)) # Round to n decimal places.
max(x) # Largest element.
sum(x)#Sum.
mean(x)#Mean.
median(x)#Median.
min(x) # Smallest element.
var(x) # The variance.
cor(x, x) # Correlation.
sd(x) # The standard deviation
t(log(x))#Natural log.
t(exp(x))# Exponential.
plot(x)
plot(wage1$educ,
wage1$wage,
xlab= 'educ',
ylab = 'wage',
col = 'blue')
curve(15+6*x -3*x^2,
xlab= 'experience',
ylab = 'income'
)
curve(15+6*x -3*x^2,
xlab= 'experience',
ylab = 'income',
col = 'purple'
)
abline(v=0.15, col="blue") # add vertical lines # change line colors
abline(h=0.40, col="red") # add horizontal lines # change line colors
abline(a = 15, b = 2, col = 'black') # add a: intercept; b: slope lines # change line colors
abline(a = 18, b = -2, col = 'gray') # add a: intercept; b: slope lines # change line colors
points(x = 0.8, y = 16.5, type = 'p' ,col="red") # x,y coordinate vectors of points to plot.
text(x=0.8,y=16.2, labels = "solution: 15, 2", col = 'green') # x,y are coordinates where the text labels should be written
# install.packages("rootSolve")
library(rootSolve)
## =======================================================================
## simultaneous equations
## =======================================================================
model <- function(x){
c(F1 = 500-0.1*x[1] - x[2],
F2 = 0.05*x[1]- x[2])
}
(ss <- multiroot(f = model, start = c(1, 1)))
what is the standard deviation function in R?
What is the way to get help in R?
Subset first element in vector x