projet-analyse-exploratoire/tp-analyse-explorative-master/tp-analyse-explorative/exo1.R
2021-12-08 18:31:26 +01:00

125 lines
3.6 KiB
R

### 1 Vectors ###
### 1.1 Let's start simple
e1 = c(2,5,0,8)
e2 = 1:200
e3 = seq(-210,-200,2)
e4 = 2^1:7
v = rep(c(1,-1),times=25)
e5 = c(e2,e3)
?seq
e6 = seq(0,1,length=70)
e7 = rep(e1,times=10)
e2-e3
### 1.2 Character vectors
# All vowels
vowels = c('a','e','i','o','u','y')
# All letters
letters = letters
# True or false if vowels are in letters
letters %in% vowels
# Index of each vowels
vowelsInLetters = which(letters %in% vowels)
# Index of each non-vowels
notVowelsInLetters = which(!(letters %in% vowels))
# Letters after vowels
lettersAfterVowels = letters[vowelsInLetters+1]
# myname with my name
myname = "Titouan"
# strsplit to extract letters
mynameSplited = strsplit(myname, NULL)
# Access its first element
mynameSplited[[1]][1]
mynameSplited[[1]]
mynameSplited[1]
# Index in alphabet of letters in my name
indexMyname = which(letters %in% mynameSplited[[1]])
# Index in alphabet of letters in my neighbour name
neighborname = "Marianne"
neighbornameSplited = strsplit(neighborname, NULL)
neighbornameSplited[[1]][1]
indexNeighborname = which(letters %in% neighbornameSplited[[1]])
# Min of average of index
mynameAverage = mean(indexMyname)
neighborAverage = mean(indexNeighborname)
minIndexOfName = min(c(mynameAverage,neighborAverage))
### 2 DataFrames ###
### 2.1 Cute Animals
# All vowels
vowels = c('a','e','i','o','u','y')
# All letters
letters = letters
# Database of 3 columns : alphabet letters, number of each letter, binary variable vowel
database = data.frame(letter=letters, index=1:26, isVowels=letters %in% vowels)
# Extracting lines corresponding to my name
indexMyname = letters %in% strsplit("Titouan", NULL)[[1]]
database[indexMyname,"index"]
# Examining msleep dataset
library(tidyverse)
head(msleep)
str(msleep)
names(msleep)
summary
# Sanity check animals either awake or asleep
which(msleep$sleep_total + msleep$awake == 24 )
# Animal that sleep the most
msleep[which.max(msleep$sleep_total),]
# Animals of less than 100g and that sleeps more than half a day
nrow(msleep[which(msleep$sleep_total > 12 & msleep$bodywt < 0.1),])
# Average brainwt/bodywt ratio (ajoute une colonne ratio)
msleep$ratio = msleep$brainwt/msleep$bodywt
mean(msleep$ratio, na.rm = TRUE)
# Animal with the highest ratio
msleep[which.max(msleep$ratio),]
### 2.2 Endangered animals
# Create a copy and reorder its factors
msleep_copy = msleep
msleep_copy$conservation = factor(x = msleep_copy$conservation, c("lc","domesticated","cd","nt","vu","en"), ordered = TRUE)
# Compare average weight of endangered animals to non-endangered
averageWtThreatened = mean(msleep_copy[which(msleep_copy$conservation > "nt"),]$bodywt)
averageWtRemaining = mean(msleep_copy[which(msleep_copy$conservation <= "nt"),]$bodywt)
# Ajoute une colonne threatened, valeur booléenne
msleep$threatened = msleep$conservation > "nt"
### 2.3 Functions
# Create a function taking a name as input and returning corresponding list of letters
lettersFromName <- function(name) {
return(strsplit(name, NULL)[[1]])
}
lettersFromName("Titouan")
# Fix with empty "" name
lettersFromName("")
lettersFromNameFixed <- function(name) {
if (name == "") {
l = NULL
}
else {
l = strsplit(name, NULL)[[1]]
}
return(l)
}
lettersFromNameFixed("")
# With non-standards characters
lettersFromNameFixed("X Æ A-12")
# Function returning corresponding genus from animal name
genusFromName <- function(name) {
genusname <- msleep[tolower(msleep$name) == tolower(name),]$genus
if (length(genusname) == 0) {
s <- c("I don't know")
}
else {
s <- c("The ",name," is a ", genusname)
}
#browser()
return(paste(s, collapse = ''))
}
genusFromName("little brown bat")