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))