7th Report
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)
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
## [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
## [,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)
merge(X1,X2)
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
* "Man","Woman","Woman")))
X
aggregate(X[,-4],by=list(Sex=X[,4]),FUN=mean)
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
## [1] 2 8 3
vec>4
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
## [1] 1 3 6 2 7 4 8 1 0 4 1
## [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)
print(women_size)
#Creating functions
BMI <- function(weight, height) {
return(imc)}
BMI(70.1.82)
## [1] 21.13271
BMI(c(70.74),c(1.82,1.90))
6
TPNOT 7o
[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
> 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)
> 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")
> mondf
xy
1 to 1
2B2
3C3
4 to 4
2. Importing data:
+ sheet="Sheet1", stringsAsFactors=TRUE)
9
New names:
• `` -> `...1`
Median:53.50 Median:163
Mean:63.66 Mean:163
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)
+ })
10
> note$level <- with(note, binVariable(Note, bins=3, method='proportions',
labels=c('weak','medium','strong')))
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
11
4. Crosstabs by hand:
By following these steps:
-Statistics > Contingency tables > Fill in and analyze a cross-sort
> 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
columns
rows Diseases
12
> .Test
data: .Table
columns
rows Diseases
> remove(.Test)
> remove(.Table)
13