0% ont trouvé ce document utile (0 vote)
3 vues15 pages

R Tutorial Code Colab Lec 2

Ce document est un tutoriel sur l'utilisation de R dans Google Colab, couvrant des concepts de base tels que les opérations arithmétiques, les fonctions, les vecteurs, les matrices, et les data frames. Il fournit des exemples de code pour illustrer chaque concept, ainsi que des instructions sur la création de fonctions personnalisées et la manipulation de données. Le tutoriel est destiné à aider les utilisateurs à se familiariser avec R et ses fonctionnalités dans un environnement de développement intégré en ligne.

Transféré par

shuvrae350
Copyright
© All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PDF, TXT ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
3 vues15 pages

R Tutorial Code Colab Lec 2

Ce document est un tutoriel sur l'utilisation de R dans Google Colab, couvrant des concepts de base tels que les opérations arithmétiques, les fonctions, les vecteurs, les matrices, et les data frames. Il fournit des exemples de code pour illustrer chaque concept, ainsi que des instructions sur la création de fonctions personnalisées et la manipulation de données. Le tutoriel est destiné à aider les utilisateurs à se familiariser avec R et ses fonctionnalités dans un environnement de développement intégré en ligne.

Transféré par

shuvrae350
Copyright
© All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PDF, TXT ou lisez en ligne sur Scribd

7/19/22, 5:31 PM R_Tutorial_Orientation.

ipynb - Colaboratory

A SHORT R TUTORIAL
Steven M. Holland
Department of Geology, University of Georgia, Athens, GA 30602-2501 24 Link:
[Link]

How to open Google colab an online IDE for R and Python, we will use R

[Link]

or

[Link]

Double-click (or enter) to edit

1
2 x = 3 + 4
3 print(x)
4 x = 2 - 9
5 print(x)
6 x = 12 * 8
7 print(x)
8 x = 16 / 4
9 print(x)
10 x = 2 ^ 3
11 print(x)

[1] 7
[1] -7
[1] 96
[1] 4
[1] 8

1 # x <- 2 * 5; everything on this line is a comment


2
3 x <- 2 * 5 # comments can also be placed after a statement
4 x

10

Spaces are generally ignored, but include them to make code easier to read. Both of these
lines produce the same result.

[Link] 1/15
7/19/22, 5:31 PM R_Tutorial_Orientation.ipynb - Colaboratory

1 x <- 3+7/14
2 x <- 3 + 7 / 14
3 x

3.5

Functions

1 max(9, 5)
2
3 x <- 100
4 max(x, 4) # objects can be arguments to functions

9
100

1 myMax <- max(9, 5) # result is stored, not displayed


2 myMax

1 help(max)
2

1 ?max

Writing your own Functions

1 pow <- function(base, exponent) {


2 result <- base^exponent
3 result
4 }
5
6 pow(2,5)

32

1 pow(base=3, exponent=2) # prints 9


2 pow(exponent=2, base=3) # also prints 9
3 pow(3,2) # also prints 9

[Link] 2/15
7/19/22, 5:31 PM R_Tutorial_Orientation.ipynb - Colaboratory

9
9
9 <- function(base, exponent=2) {
1 pow
2 result <- base^exponent
3 result
4 }
5
6 pow(5)

25

Vectors

1 x <- c(3, 7, -8, 10, 15, -9, 8, 2, -5, 7, 8, 9, -2, -4, -1)
2 x[5] # returns 15

15

1 x[3:10]

-8 · 10 · 15 · -9 · 8 · 2 · -5 · 7

1 x[c(1, 3, 5, 7)]

3 · -8 · 15 · 8

1 x[c(3:7, 5:9)]

-8 · 10 · 15 · -9 · 8 · 15 · -9 · 8 · 2 · -5

1 x[x>0] # all positive values in x

3 · 7 · 10 · 15 · 8 · 2 · 7 · 8 · 9

1 x[x<=2] # values less than or equal to 2

-8 · -9 · 2 · -5 · -2 · -4 · -1

1 x[x>5 | x<1] # values greater than 1 or less than 1

7 · -8 · 10 · 15 · -9 · 8 · -5 · 7 · 8 · 9 · -2 · -4 · -1

1 sort(x)

[Link] 3/15
7/19/22, 5:31 PM R_Tutorial_Orientation.ipynb - Colaboratory

-9 · -8 · -5 · -4 · -2 · -1 · 2 · 3 · 7 · 7 · 8 · 8 · 9 · 10 · 15
1 sort(x, decreasing=TRUE)

15 · 10 · 9 · 8 · 8 · 7 · 7 · 3 · 2 · -1 · -2 · -4 · -5 · -8 · -9

1 x <- c(1, 3, 5, 7, 9)
2 2 * x # returns 2, 6, 10, 14, 18

2 · 6 · 10 · 14 · 18

1 x <- c(1, 3, 5, 7, 9)
2 y <- c(2, 2, 2, 4, 4)
3 x + y # returns 3, 5, 7, 11, 13

3 · 5 · 7 · 11 · 13

Factors

1 habitats <- factor(c('marsh', 'grassland', 'forest', 'tundra',


2 'grassland', 'tundra', 'forest', 'marsh', 'grassland'))
3 habitats

marsh · grassland · forest · tundra · grassland · tundra · forest · marsh · grassland


Levels:

1 str(habitats)

Factor w/ 4 levels "forest","grassland",..: 3 2 1 4 2 4 1 3 2

1 y <- [Link](habitats)
2 y

'marsh' · 'grassland' · 'forest' · 'tundra' · 'grassland' · 'tundra' · 'forest' · 'marsh' · 'grassland'

Lists

1 x <- list('Hueston Woods', 42, TRUE)


2 x[[1]] # returns the first item, 'Hueston Woods'

'Hueston Woods'

[Link] 4/15
7/19/22, 5:31 PM R_Tutorial_Orientation.ipynb - Colaboratory

Matrices
1 x <- matrix(c(3, 1, 4, 2, 5, 8), nrow=3)
2 x

A
matrix:
3×2
of type
dbl
3 2

1 5

4 8

1 x[3, 2] # returns 8

1 x <- matrix(c(3, 1, 4, 2, 5, 8), nrow=3, byrow=TRUE)


2 x

A
matrix:
3×2
of type
dbl
3 1

4 2

5 8

Data Frames

1 locality <- c("Athens", "Comer", "Whitehall",


2 "Watkinsville", "Commerce")
3 beetle1 <- c(14, 23, 16, 19, 2)
4 beetle2 <- c(5, 1, 0, 0, 18)
5 beetle3 <- c(2, 3, 4, 2, 2)
6 beetle4 <- c(0, 0, 2, 3, 20)
7 oldGrowth <- c(TRUE, TRUE, TRUE, TRUE, FALSE)

1 forests <- [Link](locality, beetle1, beetle2, beetle3,


2 beetle4, oldGrowth)
3 forests

[Link] 5/15
7/19/22, 5:31 PM R_Tutorial_Orientation.ipynb - Colaboratory

A [Link]: 5 × 6
locality beetle1 beetle2 beetle3 beetle4 oldGrowth

<chr> <dbl> <dbl> <dbl> <dbl> <lgl>

Athens 14 5 2 0 TRUE

Comer 23 1 3 0 TRUE

Whitehall 16 0 4 2 TRUE

Watkinsville 19 0 2 3 TRUE

Commerce 2 18 2 20 FALSE

1 colnames(forests)

'locality' · 'beetle1' · 'beetle2' · 'beetle3' · 'beetle4' · 'oldGrowth'

1 colnames(forests) <- c('locality', '[Link]',


2 '[Link]', '[Link]', '[Link]', 'oldGrowth')

1 forests[3, 2] # third row, second column

16

1 forests$locality

'Athens' · 'Comer' · 'Whitehall' · 'Watkinsville' · 'Commerce'

1 forests[ , 2:4]

A [Link]: 5 × 3
[Link] [Link] [Link]

<dbl> <dbl> <dbl>

14 5 2

23 1 3

16 0 4

19 0 2

2 18 2

1 yMatrix <- [Link](forests)


2 yMatrix

[Link] 6/15
7/19/22, 5:31 PM R_Tutorial_Orientation.ipynb - Colaboratory

A matrix: 5 × 6 of type chr


locality [Link] [Link] [Link] [Link] oldGrowth

Athens 14 5 2 0 TRUE

Comer 23 1 3 0 TRUE

Whitehall 16 0 4 2 TRUE

Watkinsville 19 0 2 3 TRUE

Commerce 2 18 2 20 FALSE

Generating Numbers

1 seq(from=1, to=100) # integers from 1 to 100


2
3

1 · 2 · 3 · 4 · 5 · 6 · 7 · 8 · 9 · 10 · 11 · 12 · 13 · 14 · 15 · 16 · 17 · 18 · 19 · 20 · 21 · 22 · 23 · 24 ·
25 · 26 · 27 · 28 · 29 · 30 · 31 · 32 · 33 · 34 · 35 · 36 · 37 · 38 · 39 · 40 · 41 · 42 · 43 · 44 · 45 · 46 ·
47 · 48 · 49 · 50 · 51 · 52 · 53 · 54 · 55 · 56 · 57 · 58 · 59 · 60 · 61 · 62 · 63 · 64 · 65 · 66 · 67 · 68 ·
69 · 70 · 71 · 72 · 73 · 74 · 75 · 76 · 77 · 78 · 79 · 80 · 81 · 82 · 83 · 84 · 85 · 86 · 87 · 88 · 89 · 90 ·
91 · 92 · 93 · 94 · 95 · 96 · 97 · 98 · 99 · 100
1 · 3 · 5 · 7 · 9 · 11 · 13 · 15 · 17 · 19 · 21 · 23 · 25 · 27 · 29 · 31 · 33 · 35 · 37 · 39 · 41 · 43 · 45 ·
47 · 49 · 51 · 53 · 55 · 57 · 59 · 61 · 63 · 65 · 67 · 69 · 71 · 73 · 75 · 77 · 79 · 81 · 83 · 85 · 87 · 89 ·

1 seq(from=1, to=100, by=2) # odd numbers from 1 to 100


2

1 · 3 · 5 · 7 · 9 · 11 · 13 · 15 · 17 · 19 · 21 · 23 · 25 · 27 · 29 · 31 · 33 · 35 · 37 · 39 · 41 · 43 · 45 ·
47 · 49 · 51 · 53 · 55 · 57 · 59 · 61 · 63 · 65 · 67 · 69 · 71 · 73 · 75 · 77 · 79 · 81 · 83 · 85 · 87 · 89 ·
91 · 93 · 95 · 97 · 99

1 seq(from=0, to=100, by=5) # counting by fives

0 · 5 · 10 · 15 · 20 · 25 · 30 · 35 · 40 · 45 · 50 · 55 · 60 · 65 · 70 · 75 · 80 · 85 · 90 · 95 · 100

File Handling CSV

1 csv_data <- [Link](file = '/content/sample_data/[Link]')


2 print(csv_data)
3
4 # print number of columns
5 print (ncol(csv_data))
6
7 # print number of rows
8 print(nrow(csv_data))
9

[Link] 7/15
7/19/22, 5:31 PM R_Tutorial_Orientation.ipynb - Colaboratory

id name department salary projects


1 1 A HR 60754 14
2 2 B Tech 59640 3
3 3 C Marketing 69040 8
4 4 D HR 65043 5
5 5 E Tech 59943 2
6 6 F IT 65000 5
7 7 G HR 69000 7
[1] 5
[1] 7

Querying CSV Data

1 csv_data <- [Link](file ='/content/sample_data/[Link]')


2 min_pro <- min(csv_data$projects)
3 print (min_pro)
4

[1] 2

1 csv_data <- [Link](file ='/content/sample_data/[Link]')


2 new_csv <- subset(csv_data, department == "HR" & projects <10)
3 print (new_csv)
4

id name department salary projects


4 4 D HR 65043 5
7 7 G HR 69000 7

Writing into a CSV file

1 csv_data <- [Link](file ='/content/sample_data/[Link]')


2 new_csv <- subset(csv_data, department == "HR" & projects <10)
3 [Link](new_csv, "/content/sample_data/new_sample.csv")
4 new_data <-[Link](file ='/content/sample_data/new_sample.csv')
5 print(new_data)
6
7

X id name department salary projects


1 4 4 D HR 65043 5
2 7 7 G HR 69000 7

[Link] 8/15
7/19/22, 5:31 PM R_Tutorial_Orientation.ipynb - Colaboratory

Graph Plotting in R Programming


1 library(datasets)
2 data(mtcars)
3

1 head(mtcars)
2

A [Link]: 6 × 11
mpg cyl disp hp drat wt qsec vs am gear

<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>

Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4

Mazda RX4
21.0 6 160 110 3.90 2.875 17.02 0 1 4
Wag

Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4

Hornet 4
21.4 6 258 110 3.08 3.215 19.44 1 0 3
Drive

Hornet
18 7 8 360 175 3 15 3 440 17 02 0 0 3

Five Point Summary

1 summary(mtcars)

[Link] 9/15
7/19/22, 5:31 PM R_Tutorial_Orientation.ipynb - Colaboratory

mpg cyl disp hp


Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
Box Plot
Median :19.20 Median :6.000 Median :196.3 Median :123.0
Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
1 boxplot(mtcars$mpg,
drat col="green")
wt qsec vs
2 Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
Median :3.695 Median :3.325 Median :17.71 Median :0.0000
Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
am gear carb
Min. :0.0000 Min. :3.000 Min. :1.000
1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
Median :0.0000 Median :4.000 Median :2.000
Mean :0.4062 Mean :3.688 Mean :2.812
3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
Max. :1.0000 Max. :5.000 Max. :8.000

Histograms

1 hist(mtcars$mpg, col = "green") ## Plot 1


2
3

[Link] 10/15
7/19/22, 5:31 PM R_Tutorial_Orientation.ipynb - Colaboratory

1 hist(mtcars$mpg, col = "green", breaks = 25) ## Plot 2


2

1 hist(mtcars$mpg, col = "green", breaks = 50) ## Plot 3

[Link] 11/15
7/19/22, 5:31 PM R_Tutorial_Orientation.ipynb - Colaboratory

Bar Plotting

1 barplot(table(mtcars$carb), col="green")
2

Scatter Plot

1 with(mtcars, plot(mpg, qsec))


2
[Link] 12/15
7/19/22, 5:31 PM R_Tutorial_Orientation.ipynb - Colaboratory

1 plot(1:10, main="My Graph", xlab="The x-axis", ylab="The y axis")

1 plot(c(1,2,3,4,5,6), c(2,4,6,8,10,12))

[Link] 13/15
7/19/22, 5:31 PM R_Tutorial_Orientation.ipynb - Colaboratory

1 plot(c(1,2,3,4,5,6), c(1,2,4,8,16,32), type = 'l')

[Link] 14/15
7/19/22, 5:31 PM R_Tutorial_Orientation.ipynb - Colaboratory

[Link] 15/15

Vous aimerez peut-être aussi