Summary Sheet of R codes to bring the day on the exam
1 view 0 purchase
Course
Introduction to R (NWIBB095)
Institution
Radboud Universiteit Nijmegen (RU)
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.
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
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller mireiasabater24. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $6.02. You're not tied to anything after your purchase.