100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.6 TrustPilot
logo-home
Summary

Summary Sheet of R codes to bring the day on the exam

Rating
-
Sold
3
Pages
2
Uploaded on
11-12-2024
Written in
2024/2025

1 sheet paper with all the R codes used in the course to bring it on the day of the exam. It has also examples using the codes, that are very similar to the exercises of the exam.

Institution
Course








Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Institution
Study
Course

Document information

Uploaded on
December 11, 2024
Number of pages
2
Written in
2024/2025
Type
Summary

Subjects

Content preview

UNIT 1 + 2: BASICS

Environment ls()
Remove: remove(unique_co2)
Remove all: rm(list=ls())
Operations Sum: sum(B,na.rm=TRUE)
Round: round(sqrt(42),digits=1)
Mean: mean(r)
Vector (1D) A<-c(2,4,6,8,10,12,14,16,18,20)
Replace value in a vector: B[2]<-1
Matrix (2D same class type) A<-matrix(c(1:9),nrow=3, ncol=3,byrow=TRUE)
Replace elements in a matrix: Q["Rafael",]<-1
Data frame (3D) Nile_data <- data.frame(Year = c(1871:1970), Flow = Nile)
Replace elements in a data frame: lb19[1,1]<-"Sara"
List (different class types) trees_list<-list(v_one=trees_heights,v_two=trees_names,df=trees_data_frame)
Row and column names rownames(merge_Nile_temp)<-merge_Nile_temp$Year
colnames(iris)[c(1:5)]<-c("Sepal.L","sepal.W","Petal.L","Petal.W","Species")
class class(First<-FALSE)
Convert to a numeric object: as.numeric(B)
Convert to a logical object: as.logical(B_numeric+A)
Convert to a data frame: as.data.frame(my_data)
Convert to a factor: data_savanna$SPP <- as.factor(data_savanna$SPP)
Convert to a character: trees<-as.character(unique(Orange$Tree))
UNIT 3 + 4: DATA MANAGEMENT

Set working directory setwd("C:/Users/misab/OneDrive/Documentos/BIOMED/4rt curs/Introduction to R/my_WD")
Get working directory: getwd()
Overview of the files: list.files("my_WD")
Create in the working directory Folders: dir.create("my_WD/Raw_Data")
Copy file: file.copy(“my_WD/test.txt”,to=”my_WD/Raw_Data”)
Delete file: file.remove("my_WD/test.txt")
Save in working directory CSV: write.csv(iris,file="Raw_data/iris.csv", row.names=FALSE)
RDS: saveRDS(B,file="Raw_Data/B.rds")
RData: save(lb19,lb20,lb,file="Derived_data/LU3_7_8.RData")
Load data to R CSV: my_data<-read.csv("Raw_Data/my_data.csv",header=FALSE, sep=";")
RDS: readRDS(file="Raw_Data/B.rds")
RData (read only names): lbd<-load("Derived_data/LU3_7_6.RData")
Load data from R data(iris)
information: ?iris
Data information str(iris)
head(iris_width)
Add a new column iris$Flower_ID<-rownames(iris)
Replace elements of a column: students_clean$lang_correct[students_clean$class==10980]<-
students_clean$lang_correct[students_clean$class==10980]+1
Paste rownames(iris) <- paste("flower", rownames(iris), sep="_")
Change to lower case letters mammals_data_combined$kingdom_name <- tolower(mammals_data_combined$kingdom_name)
Missing values (NA) any(is.na(iris))
Sum: sum(is.na(airquality$Ozone))
Remove: iris_no_na<-iris[complete.cases(iris),]
Duplicates duplicated(co2)
anyDuplicated(IUCN_combined_unique)
any(duplicated(students$studentID))
Remove: unique_co2<-unique(co2)
Merge mammals_data_combined<-
merge(x=IUCN_mammals,y=mammalsFunctionalData,by="scientific_name",all=FALSE)
Bind Columns: cbind(countries_iris,head_iris_width)
Rows: IUCN_combined<-data.frame(rbind(IUCN_mammals,IUCN_primates_not_lemuridae))
Subset Equal: esoph[esoph$ncases==0,]
More than: subset(esoph,ncontrols>2*ncases)
More or equal to: subset(esoph,ncontrols>=10)
Less than: <
Less or equal to: <=
Not to: subset(esoph, !ncontrols<=2*ncases)
And: esoph[esoph$tobgp=="30+"&esoph$ncases=="0",]
Or: esoph[esoph$tobgp=="0-9g/day"|esoph$alcgp=="0-39g/day",]
Select rows/ columns: esoph[c(1:4),c(4:5)]
Not select rows/ columns: IUCN_mammals_removed<-IUCN_mammals[-
seq(from=10,to=nrow(IUCN_mammals), by =10),-c(3,6,9)]
Inside: students_clean[students_clean$lang%in%box_lang$out,]
Sequence esoph[seq(from=1,to=4,by=1),seq(from=1,to=5,by=2)]
Order esoph[order(esoph$agegp,esoph$ncases,decreasing = TRUE),]
Conditional (if else) +2 answers: if(esoph$ncontrols[6]>esoph$ncontrols[38]){
print("observation 6 has more number of controls than observation 38")
}
else if(esoph$ncontrols[6]<esoph$ncontrols[38]){
print("observation 6 has fewer number of controls than observation 38")
}
else{
print(“observation 6 has the same number of controls than observation 38”)
}
2 answers: esoph$twice_nr_controls<-ifelse(esoph$ncontrols>2*esoph$ncases,"Yes","No")
Which (categorical), if (numerical) rowsOmnivore <- which(mammals_data_combined_subset$Diet_description == "Omnivore")
if (length(rowsOmnivore) > 0) {
print(mammals_data_combined_subset[rowsOmnivore, c("genus_name", "Species")])
}
else {
print("None of these species is an omnivore")
}
UNIT 5: DATA ANALYSIS

Plot operations All: summary(InsectSprays)
Median: median(InsectSprays$count)
Quantile: quantile(InsectSprays$count,probs = c(0.25,0.75))
$7.43
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
mireiasabater24

Get to know the seller

Seller avatar
mireiasabater24 Radboud Universiteit Nijmegen
Follow You need to be logged in order to follow users or courses
Sold
11
Member since
1 year
Number of followers
0
Documents
7
Last sold
1 week ago

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions