Skip to content

Commit

Permalink
applied new template and reorganized a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
WandrilleD committed Jan 3, 2024
1 parent b72dffc commit 9d6c6b6
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 85 deletions.
Binary file modified assets/pptx/First-steps-with-R_day1.pptx
Binary file not shown.
94 changes: 45 additions & 49 deletions code/day1_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,25 @@ height_in_cm <- c(Mia=180, Paul=167, Ed=199)
height_in_cm

height_in_cm[3] # get the first element
height_in_cm[c(1,3)] # get the 1st and 3rd element
height_in_cm["Paul"] # get the element named "Paul"

height_in_cm[c(1,3)] # get the 1st and 3rd element

## creating vectors II

numeric(5); character(4); logical(4)

a <- 1:10
a

s <-seq(4,10,2) # from 4 to 10, by steps of 2
s <-seq(0,2,0.5) # from 0 to 2, by steps of 0.5
s

c <- rep("WT", 3 )
c

b <- rep("KO", 3 )
b
genotypes <- rep( c('WT','KO') , 3 )​
genotypes

## c() stands from concatenate. you can use it to group vectors
c(c,b)
genotypes <- c(rep("WT",3), rep("KO",3))
c(a,s)


## trick : with [] you can extract element repeatedly
tplayer <- c("Federer", "Nadal")
Expand Down Expand Up @@ -67,26 +64,15 @@ y
sum(x) # number that are true
mean(x) # proportion that are true

# Factors
genotype <- factor(c("WT", "WT", "Mut2", "Mut1", "Mut2"))
genotype
levels(genotype)

geno <- as.character(genotype)
geno

# Factors II
genotype <- factor(c("WT", "WT", "Mut2", "Mut1", "Mut2"),levels=c("WT","Mut1","Mut2"))

genotype <- c("WT", "WT", "Mut2", "Mut1", "Mut2")
genotype
levels(genotype)

## logical operator

## == -> check equality
c(1,3,2) == 2

c(1,3,2) < 2

## ! inverses TRUE and FALSE
! (c(1,3,2) < 2)

Expand Down Expand Up @@ -143,6 +129,18 @@ poets <- data.frame(name, status, reader_rating)
poets


poets[2, 2] # gets the element on row 2 in column 2
poets[, c(1,3)] # gets columns 1 and 3
poets[, c("name", "reader_rating")] # gets columns "name" and "reader_rating"
poets$name # gets column "name"

## names
rownames(poets) # gets the row names
colnames(poets) # gets the column names

rownames(poets) <- c("J", "C", "H") # overwrites row names
poets

## list
vec <- c(0.4, 0.9, 0.6)
mat <- cbind(c(1,1), c(2,1))
Expand All @@ -154,42 +152,22 @@ l
l_with_names <- list(myvector=vec, mymatrix=mat, mydata=df)
l_with_names



## access elements

# matrix:
mat
mat[2, 2] # gets the element on row 2 in column 2
mat[1:2, ] # gets rows 1,2
mat[, c(1,4)] # gets columns 1 and 4

# data frame:
poets
poets[2, 2] # gets the element on row 2 in column 2
poets[, c(1,3)] # gets columns 1 and 3
poets[, c("name", "reader_rating")] # gets columns "name" and "reader_rating"
poets$name # gets column "name"

# list:
# access elements:
l
l[[1]] # gets the first object
l_with_names[["myvec"]]# gets the object named "myvec"
l_with_names$myvec # gets the object named "myvec", too


## names
rownames(poets) # gets the row names
colnames(poets) # gets the column names

rownames(poets) <- c("J", "C", "H") # overwrites row names
poets

# list:
names(l_with_names) # gets the list elements' names
names(l_with_names) <- c("A", "B", "C") # overwrites names






### File

file.path("C:", "Users", "Leo", "courses", "data", "snp.csv")
Expand All @@ -213,6 +191,22 @@ str( snps )
summary( snps )


# Factors --> not here anymore
genotype <- factor(c("WT", "WT", "Mut2", "Mut1", "Mut2"))
genotype
levels(genotype)

geno <- as.character(genotype)
geno

# Factors II
genotype <- factor(c("WT", "WT", "Mut2", "Mut1", "Mut2"),levels=c("WT","Mut1","Mut2"))

genotype <- c("WT", "WT", "Mut2", "Mut1", "Mut2")
genotype
levels(genotype)


snps$chr <-factor(snps$chr)
snps$minor <-factor(snps$minor)
snps$major <-factor(snps$major)
Expand All @@ -232,7 +226,9 @@ subset(snps, chr==1 & major=="A")
subset(snps, chr==1 & (major=="A" | major=="T"))

## tapply
tapply(X=snps$pos, INDEX=snps$chr, FUN=min)
data(sleep)
head(sleep)
tapply(X=sleep$extra, INDEX=sleep$group, FUN=mean)

## adding rows
snps_updated <- rbind(snps,
Expand Down
Binary file modified slides/First-steps-with-R_day1.pdf
Binary file not shown.
46 changes: 23 additions & 23 deletions solutions/R_practice4_solution.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# 1) Create two vectors, "vector_a" and "vector_b", containing the values from −5 to 5 and from 10 down to 0, respectively.
vector_a <- -5:5
vector_b <- seq(10,0) # alternatively: vector_b <- c(10,9,8,7,6,5,4,3,2,1,0)

# 2) Calculate the (elementwise) sum, difference and product between the elements of "vector_a" and "vector_b".
vector_a + vector_b #sum
vector_a - vector_b #difference
vector_a * vector_b #product

# 3) a) Calculate the sum of elements in "vector_a"
# b) Calculate the overall sum of elements in both "vector_a" and "vector_b".
sum(vector_a)
sum(vector_a, vector_b) # alternatively : sum(vector_a + vector_b)


# 4) Identify the smallest and the largest value among both "vector_a" and "vector_b".
min(vector_a, vector_b)
max(vector_a, vector_b)

# 5) Compute the overall mean of the values among both "vector_a" and "vector_b"
mean( c( vector_a, vector_b) ) # mean() works only on a single vector, unlike sum, min and max!
# Concatenate both vectors (using c() ) before computing the mean

# 1) Create two vectors, "vector_a" and "vector_b", containing the values from −5 to 5 and from 10 down to 0, respectively.
vector_a <- -5:5
vector_b <- seq(0,1,0.1) # alternatively (0:10)*0.1

# 2) Calculate the (elementwise) sum, difference and product between the elements of "vector_a" and "vector_b".
vector_a + vector_b #sum
vector_a - vector_b #difference
vector_a * vector_b #product

# 3) a) Calculate the sum of elements in "vector_a"
# b) Calculate the overall sum of elements in both "vector_a" and "vector_b".
sum(vector_a)
sum(vector_a, vector_b) # alternatively : sum(vector_a + vector_b)


# 4) Identify the smallest and the largest value among both "vector_a" and "vector_b".
min(vector_a, vector_b)
max(vector_a, vector_b)

# 5) Compute the overall mean of the values among both "vector_a" and "vector_b"
mean( c( vector_a, vector_b) ) # mean() works only on a single vector, unlike sum, min and max!
# Concatenate both vectors (using c() ) before computing the mean

20 changes: 7 additions & 13 deletions solutions/R_practice5_solution.R
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@

# 1)Install and load the package MASS (or other CRAN packages).
# install.packages("MASS")
# 1) The following command lines loads the MASS library and the bacteria data.frame present in the MASS package. Execute it:
library(MASS)


# 2) The following command line loads the bacteria data.frame present in the MASS package. Execute it:

data(bacteria)
?bacteria

# 3) What are the names of the columns of the bacteria data.frame ?
# 2) What are the names of the columns of the bacteria data.frame ?

names(bacteria)

# 4) Use the [] , to select in bacteria rows 100 to 119 in the column "ap".
# 3) Use the [] , to select in bacteria rows 100 to 119 in the column "ap".

bacteria[ 100:119 , "ap" ]

# 5) Use $ to get the column "week" and check how many missing values it has.

sum(is.na(bacteria$week))
# 4) Use $ to get the column "week" and check how many 0 values it has.

#Optional : 6) use comparison operators to count how many rows correspond to a “placebo” treatment (“trt” column).
sum(bacteria$week == 0)

sum(bacteria$trt == "placebo")
# 5) Optional : using a comparison operator and [], select the rows which correspond to a “placebo” treatment (in the “trt” column).

bacteria[ bacteria$trt == "placebo" , ]

0 comments on commit 9d6c6b6

Please sign in to comment.