Вы находитесь на странице: 1из 6

R Notebook

Today is Feb 5th and we are flipping!


1:4 # create a sequence

## [1] 1 2 3 4
-2:2

## [1] -2 -1 0 1 2
exp(-2:2)

## [1] 0.1353353 0.3678794 1.0000000 2.7182818 7.3890561


1:4+exp(-2:2)

## Warning in 1:4 + exp(-2:2): longer object length is not a multiple of


## shorter object length
## [1] 1.135335 2.367879 4.000000 6.718282 8.389056
this is a new chunk
help(pi)
pi

## [1] 3.141593
LETTERS

## [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q"
## [18] "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
pi>10

## [1] FALSE
all together
exp(-2:2) > 5

## [1] FALSE FALSE FALSE FALSE TRUE


sum(exp(-2:2) > 5)

## [1] 1
bigger-or-equal test is “>=”
d <- read.table("http://virgo.unive.it/paolop/papers/grades.txt")
str(d)

## 'data.frame': 167 obs. of 1 variable:


## $ V1: Factor w/ 53 levels "-1","-1.5","0",..: 53 17 41 41 38 22 11 34 33 35 ...
d2 <- read.table("http://virgo.unive.it/paolop/papers/grades.txt",header=T)
str(d2)

## 'data.frame': 166 obs. of 1 variable:


## $ grade: num 16 28.5 28.5 26.5 18.5 13 24 23.5 24.5 3 ...
how to extract the grades from d2? use the dollar operator

1
d2$grade

## [1] 16.0 28.5 28.5 26.5 18.5 13.0 24.0 23.5 24.5 3.0 11.0 16.0 16.5 15.5
## [15] 24.0 22.5 31.0 31.0 18.5 8.5 17.0 18.5 29.0 26.5 14.5 28.5 18.0 31.0
## [29] 13.5 29.0 18.0 25.0 21.0 13.0 12.5 31.0 -1.0 31.0 3.5 24.0 22.0 31.0
## [43] 9.5 26.0 19.5 13.5 9.5 17.0 31.0 26.5 22.0 23.5 19.5 31.0 31.0 26.5
## [57] 28.5 12.5 26.0 28.0 24.5 0.0 16.5 4.5 24.0 28.5 12.0 24.0 29.0 5.0
## [71] 24.0 20.5 4.5 13.0 -1.0 18.5 31.0 15.0 31.0 11.0 5.0 26.0 20.0 29.0
## [85] 18.0 18.0 31.0 7.5 14.0 24.0 19.0 0.0 31.0 26.0 22.0 26.0 10.0 11.5
## [99] 15.5 24.0 24.0 -1.5 26.0 23.5 18.0 23.5 21.0 17.5 5.0 18.0 14.5 8.5
## [113] 10.5 18.5 15.0 16.5 3.0 23.5 13.5 1.5 24.5 21.5 26.5 26.0 14.5 31.0
## [127] 31.0 2.5 3.0 24.0 24.0 15.5 24.0 26.5 28.5 10.0 10.5 29.0 17.5 22.0
## [141] 15.0 12.5 19.5 22.0 10.5 28.5 31.0 28.5 8.5 31.0 18.5 16.5 26.0 13.0
## [155] 27.0 31.0 17.0 22.0 9.0 2.0 5.5 4.5 22.0 26.5 31.0 29.0
sum(d2$grade>=18)

## [1] 100
one more step: how do i extract proper substes of data?
passing_grades <- d2$grade[d2$grade>=18]
passing_grades

## [1] 28.5 28.5 26.5 18.5 24.0 23.5 24.5 24.0 22.5 31.0 31.0 18.5 18.5 29.0
## [15] 26.5 28.5 18.0 31.0 29.0 18.0 25.0 21.0 31.0 31.0 24.0 22.0 31.0 26.0
## [29] 19.5 31.0 26.5 22.0 23.5 19.5 31.0 31.0 26.5 28.5 26.0 28.0 24.5 24.0
## [43] 28.5 24.0 29.0 24.0 20.5 18.5 31.0 31.0 26.0 20.0 29.0 18.0 18.0 31.0
## [57] 24.0 19.0 31.0 26.0 22.0 26.0 24.0 24.0 26.0 23.5 18.0 23.5 21.0 18.0
## [71] 18.5 23.5 24.5 21.5 26.5 26.0 31.0 31.0 24.0 24.0 24.0 26.5 28.5 29.0
## [85] 22.0 19.5 22.0 28.5 31.0 28.5 31.0 18.5 26.0 27.0 31.0 22.0 22.0 26.5
## [99] 31.0 29.0
question: can i extract grades from 22 to 26 included?
bigger22 <- d2$grade>=22
smaller26 <- d2$grade<=26
grades2226 <- d2$grade[bigger22 & smaller26]
grades2226

## [1] 24.0 23.5 24.5 24.0 22.5 25.0 24.0 22.0 26.0 22.0 23.5 26.0 24.5 24.0
## [15] 24.0 24.0 26.0 24.0 26.0 22.0 26.0 24.0 24.0 26.0 23.5 23.5 23.5 24.5
## [29] 26.0 24.0 24.0 24.0 22.0 22.0 26.0 22.0 22.0
how to show a graph?
hist(grades2226)

2
Histogram of grades2226
12
10
8
Frequency

6
4
2
0

22 23 24 25 26

grades2226
hist(d2$grade)

Histogram of d2$grade
30
25
Frequency

20
15
10
5
0

0 10 20 30

d2$grade
how to use the if sentence? let’s ploat a function y xˆ2
x <- seq(-3,3,length=21)
x

3
## [1] -3.0 -2.7 -2.4 -2.1 -1.8 -1.5 -1.2 -0.9 -0.6 -0.3 0.0 0.3 0.6 0.9
## [15] 1.2 1.5 1.8 2.1 2.4 2.7 3.0
x <- seq(-3,3,len=21)
x

## [1] -3.0 -2.7 -2.4 -2.1 -1.8 -1.5 -1.2 -0.9 -0.6 -0.3 0.0 0.3 0.6 0.9
## [15] 1.2 1.5 1.8 2.1 2.4 2.7 3.0
y <- x^2
plot(x,y)
8
6
y

4
2
0

−3 −2 −1 0 1 2 3

x
if(2>3) "ciao" else "miao"

## [1] "miao"
if(c(T,F)) "ciao" else "miao"

## Warning in if (c(T, F)) "ciao" else "miao": the condition has length > 1
## and only the first element will be used
## [1] "ciao"
if(c(F,T)) "ciao" else "miao"

## Warning in if (c(F, T)) "ciao" else "miao": the condition has length > 1
## and only the first element will be used
## [1] "miao"
use a new function when many ifs are to be performed
# ifelse(vb,vt,vf)
ifelse(c(T,F),"ciao","miao")

## [1] "ciao" "miao"


exercise 7

4
tent <- function(x) if(x<0.5) 2*x else 2-2*x
tent(1/4)

## [1] 0.5
tent2 <- function(x) ifelse(x<0.5,2*x,2-2*x)
tent2(1/4)

## [1] 0.5
tent(c(1/4,1/2,3/4))

## Warning in if (x < 0.5) 2 * x else 2 - 2 * x: the condition has length > 1


## and only the first element will be used
## [1] 0.5 1.0 1.5
tent2(c(1/4,1/2,3/4))

## [1] 0.5 1.0 0.5


this was typed after the end of the lecture, when a student asked me about “sumfraction
sumfraction <- function(r,s,t){
num <- r*t+s
den <- t
if(t==0) "errore" else c(num,den)
}
sumfraction(1,2,3)

## [1] 5 3
sumfraction(2,1,0)

## [1] "errore"
what you see below is the standard text filling the template o notebook.
This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath
the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and
pressing Cmd+Shift+Enter.
plot(cars)

5
100 120
80
dist

60
40
20
0

5 10 15 20 25

speed
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Cmd+Option+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click
the Preview button or press Cmd+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit,
Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor
is displayed.

Вам также может понравиться