0% found this document useful (0 votes)
11 views14 pages

7th Report

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views14 pages

7th Report

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

7th REPORT

PR.TOUMI

OUCHAOU Linda
Table of Contents
1. Operations on vectors, matrices and lists:.............................................................................................2
2. Functions:............................................................................................................................................................. 2
3. The recycling:..................................................................................................................................................... 2
4. Basic functions:.................................................................................................................................................. 2
5. Merging tables and Merge:........................................................................................................................... 3
6. Organize data for ANOVA:.............................................................................................................................3
7. The aggregate() function:..............................................................................................................................4
8. The transform() function:............................................................................................................................. 4
9. Extraction/Insertion into vectors:............................................................................................................ 5
a. Extraction:........................................................................................................................................................ 5
b. Insertion:........................................................................................................................................................... 5
10. Exercise:............................................................................................................................................................ 6
TPNOTo7......................................................................................................................................................................... 7
1. Carry out the orders made in TP 1:...........................................................................................................7
a. R like a calculator:......................................................................................................................................... 7
b. Items:.................................................................................................................................................................. 7
c. Vectors:.............................................................................................................................................................. 8
d. BMI:..................................................................................................................................................................... 8
e. Dataframe creation:..................................................................................................................................... 9
2. Importing data:.................................................................................................................................................. 9
a. Import an excel file:......................................................................................................................................9
b. Create a data summary:...........................................................................................................................10
3. Convert variables:.......................................................................................................................................... 10
a. Converting numerical values to one factor:....................................................................................10
..................................................................................................................................................................................... 11
b. Graphical representation of data:....................................................................................................... 11
c. Splitting a numeric variable in class:.................................................................................................12
4. Crosstabs by hand:........................................................................................................................................ 13

1
1. Operations on vectors, matrices and lists:
x <- c(1,2,4,6,3)
y <- c(4,7,8,1,1)
x+y

## [1] 5 9 12 7 4

2. Functions:
M <- matrix(1:9,nrow=3)
exp(M)

## [,1] [,2] [,3]


## [1,] 2.718282 54.59815 1096.633
## [2,] 7.389056 148.41316 2980.958
## [3,] 20.085537 403.42879 8103.084

3. The recycling:
x <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
y <- c(1,2,3,4,5,6,7,8,9,10)
x+y

## Warning in x + y: the size of a longer object is not a multiple of the


## size of a shorter object

## [1] 2 4 6 8 10 12 14 16 18 20 12 14 16 18 20

4. Basic functions:
length(c(1,3,6,2,7,4,8,1,0))

## [1] 9

sort(c(1,3,6,2,7,4,8,1,0))

## [1] 0 1 1 2 3 4 6 7 8

sort(c(1,3,6,2,7,4,8,1,0),decreasing=TRUE)

## [1] 8 7 6 4 3 2 1 1 0

rev(c(1,3,6,2,7,4,8,1,0))

## [1] 0 1 8 4 7 2 6 3 1

2
vec <- c(1, 3, 6, 2, 7, 4, 8, 1, 0)
names(vec) <- 1:9
ith

## 1 2 3 4 5 6 7 8 9
## 1 3 6 2 7 4 8 1 0

unique(c(1,3,6,2,7,4,8,1,0))

## [1] 1 3 6 2 7 4 8 0

duplicated(c(1,3,6,2,7,4,8,1,0))

## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE

5. Merging tables and Merge:


cbind(1:4,5:8)

## [,1] [,2]
## [1,] 1 5
## [2,] 2 6
## [3,] 3 7
## [4,] 4 8

X1 <- data.frame(Id=1:4,SEX=c("H","F","F","H"),
Weight=c(75,68,48,72))
X2 <- data.frame(Id=1:4,SEX=c("H","F","F","H"),
Size=c(182,165,160,178))
cbind(X1,X2)

## Id SEX Weight Id SEX Height


## 1 1 H 75 1 H 182
## 2 2 F 68 2 F 165
## 3 3 F 48 3 F 160
## 4 4:72 4:178

merge(X1,X2)

## Id SEX Weight Height


## 1 1 H 75 182
## 2 2 F 68 165
## 3 3 F 48 160
## 4 4 H 72 178

6. Organize data for ANOVA:


X <- data.frame(trt1=c(1,6,3,5),trt2=c(8,8,3,1))
X

3
## trt1 trt2
## 1 1 8
## 2 6 8
## 3 3 3
## 4 5 1

stack(X)

## values ind
## 1 1 trt1
## 2 6 trt1
## 3 3 trt1
## 4 5 trt1
## 5 8 trt2
## 6 8 trt2
## 7 3 trt2
## 8 1 trt2

7. The aggregate() function:

* "Man","Woman","Woman")))
X

## Weight Height Cholesterol sex


## 1 80 180 44 Male
## 2 75 170 12 Male
## 3 60 165 23 Female
## 4 52 150 34 Female

aggregate(X[,-4],by=list(Sex=X[,4]),FUN=mean)

## Sex Weight Height Cholesterol


## 1 Female 56.0 157.5 28.5
## 2 Male 77.5 175.0 28.0

8. The transform() function:


X <- transform(X,Height=Height/100,BMI=Weight/(Height/100)^2)
X

## Weight Height Cholesterol sex BMI


## 1 80 1.80 44 Male 24.69136
## 2 75 1.70 12 Male 25.95156
## 3 60 1.65 23 Female 22.03857
## 4 52 1.50 34 Female 23.11111

4
9. Extraction/Insertion into vectors:
a. Extraction:
vec <- c(2,4,6,8,3)
vec[2]

## [1] 4

"["(with,2)

## [1] 4

vec[-2]

## [1] 2 6 8 3

ith[2:5]

## [1] 4 6 8 3

vec[-c(1,5)]

## [1] 4 6 8

vec <- c(2, 4, 6, 8, 3)


vec[c(TRUE, FALSE, FALSE, TRUE, TRUE)]

## [1] 2 8 3

vec>4

## [1] FALSE FALSE TRUE TRUE FALSE

vec[vec>4]

## [1] 6 8

b. Insertion:
vecA <- c(1, 3, 6, 2, 7, 4, 8, 1, 0)
vecA

## [1] 1 3 6 2 7 4 8 1 0

(vecB <- c(vecA, 4, 1))

## [1] 1 3 6 2 7 4 8 1 0 4 1

(vecC <- c(vecA[1:4], 8, 5, vecA[5:9]))

## [1] 1 3 6 2 8 5 7 4 8 1 0

5
a <- c()
a <- c(a,2)
a <- c(a,7)
has

## [1] 2 7

10. Exercise:
# Create the size and gender vectors
size <- c(182,150,100,140.5)
gender <- c(0,1,0,1,0)

# Extract men's sizes


men_size <- size[sex == 1]

# Extract women's sizes


size_women <- size[sex == 0]

# Show the results


print(men_size)

## [1] 150 140

print(women_size)

## [1] 182 100 5

#Creating functions
BMI <- function(weight, height) {

bmi <- weight/height^2

return(imc)}

BMI(70.1.82)

## [1] 21.13271

BMI(c(70.74),c(1.82,1.90))

## [1] 21.13271 20.49861

6
TPNOT 7o

1. Carry out the orders made in TP 1:


a. R like a calculator:
>2+3

[1] 5

> 8 - 12

[1] -4

>14*25

[1] 350

> -3/10

[1] -0.3

b. Items:
> x <- 2

>x+3

[1] 5

>x

[1] 2

> x <- 27

> y <- 10

> A<- x + y

>A

[1] 37

> x <- “Hello”

> A<- x

>A

7
[1] “Hello”

c. Vectors:
> sizes <- c(167, 192, 173, 174, 172, 167, 171, 185, 163

+ +170)

> sizes

[1] 167 192 173 174 172 167 171 185 333

> sizes

[1] 167 192 173 174 172 167 171 185 333

> sizes

[1] 167 192 173 174 172 167 171 185 333

> sizes <- c(167, 192, 173, 174, 172, 167, 171, 185, 163, 170)

> sizes + 20

[1] 187 212 193 194 192 187 191 205 183 190

> sizes/100

[1] 1.67 1.92 1.73 1.74 1.72 1.67 1.71 1.85 1.63 1.70

> sizes^2

[1] 27889 36864 29929 30276 29584 27889 29241 34225 26569 28900

d. BMI:
> sizes <- c(167, 192, 173, 174, 172, 167, 171, 185, 163, 170)

> weight <- c(86, 74, 83, 50, 78, 66, 66, 51, 50, 55)

> sizes.m <- sizes/100

> bmi <- weight/(heights.m^2)

> imc

[1] 30.83653 20.07378 27.73230 16.51473 26.36560 23.66524 22.57105 14.90139 18.81892 19.03114

> answer <- c("Bac+2", "Bac", "CAP", "Bac", "Bac", "CAP", "BEP")

8
> answer <- c("Bac+2", "Bac", "CAP", "Bac", "Bac", "CAP", "BEP")

> answer[2]

[1] “Bac”

> sizes <- c(167, 192, 173, 174, 172, 167, 171, 185, 163, 170)

> length(sizes)

[1] 10

> mean(sizes)

[1] 173.4

> var(sizes)

[1] 76.7111

e. Dataframe creation:
> x <- c("A","B","C","A")

> y <- 1:4

> mondf <- data.frame(x,y)

> mondf

xy

1 to 1

2B2

3C3

4 to 4

2. Importing data:

a. Import an excel file:


> Dataset <- readXL("C:/Users/ADMIN/Desktop/tp7/WEIGHT SIZE.xlsx",
rownames=FALSE, header=TRUE, na="",

+ sheet="Sheet1", stringsAsFactors=TRUE)

9
New names:

• `` -> `...1`

b. Create a data summary:


> summary(Dataset)

...1 WEIGHT SIZE

F:3 Min. :49.20 Min. :151

H:2 1st Qu.:52.60 1st Qu.:160

Median:53.50 Median:163

Mean:63.66 Mean:163

3rd Qu.:74.40 3rd Qu.:169

Max. :88.60 Max. :172

3. Convert variables:
a. Converting numerical values to one factor:
> note <- readXL("C:/Users/ADMIN/Desktop/tp7/note.xlsx", rownames=FALSE, header=TRUE, na="",
sheet="Sheet1", stringsAsFactors=TRUE)

> save("note", file="C:/Users/ADMIN/Desktop/tp7/note.RData")

> note <- within(note, {

+ Sex <- factor(Sex, labels=c('girl','boy'))

+ })

10
> note$level <- with(note, binVariable(Note, bins=3, method='proportions',
labels=c('weak','medium','strong')))

b. Graphical representation of data:


To create the graph; we follow the following steps:
Graphs > Pie chart and we will then have the following command:

with(note, piechart(level, xlab="level", ylab="sex", main="the level according to the sex of the students",
col=rainbow_hcl(3), scale="frequency"))

c. Sp
lit
ti
n
g
a

numeric variable in class:


- By following these steps:
Statistics > Data > Decode a numeric variable in class
> note$level <- with(note, binVariable(Note, bins=3, method='proportions', labels=c('Note1','note2','note3')))

11
4. Crosstabs by hand:
By following these steps:
-Statistics > Contingency tables > Fill in and analyze a cross-sort

> .Table <- matrix(c(45,55,20,80), 2, 2, byrow=TRUE)

> dimnames(.Table) <- list("rows"=c("G1 variant", "No G1 variant"), "columns"=c("Diseases X", "Healthy"))

> .Table#Counts

columns

rows Diseases

G1 variant 45 55

No G1 variant 20 80

> colPercents(.Table) # Column Percentages

columns

rows Diseases

G1 variant 69.2 40.7

No G1 variant 30.8 59.3

Total 100.0 100.0

Count 65.0 135.0

> .Test <- chisq.test(.Table, correct=FALSE)

12
> .Test

Pearson's Chi-squared test

data: .Table

X-squared = 14.245, df = 1, p-value = 0.0001605

> round(.Test$residuals^2, 2) # Chi-square Components

columns

rows Diseases

G1 variant 4.81 2.31

No G1 variant 4.81 2.31

> remove(.Test)

> remove(.Table)

13

You might also like