0% found this document useful (0 votes)
21 views

R Function Cheat Sheet

This document provides a cheat sheet of common R commands for working with biomedical data and packages. It includes commands for: 1) Setting the working directory, deleting files, getting help, and saving/loading workspaces and histories. 2) Installing common biomedical data analysis packages from Bioconductor like affy, multtest, and genefilter using the BiocManager package. 3) Performing essential data operations like importing data, determining dimensions, structure and variable names, transforming and plotting data, and exporting results.

Uploaded by

kate
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

R Function Cheat Sheet

This document provides a cheat sheet of common R commands for working with biomedical data and packages. It includes commands for: 1) Setting the working directory, deleting files, getting help, and saving/loading workspaces and histories. 2) Installing common biomedical data analysis packages from Bioconductor like affy, multtest, and genefilter using the BiocManager package. 3) Performing essential data operations like importing data, determining dimensions, structure and variable names, transforming and plotting data, and exporting results.

Uploaded by

kate
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

ALS411

Biomedical Data Systems and Informatics

R commands Cheat Sheet


Basic Command Line Functions in R

setwd(“~Desktop/FolderX”) Sets working directory to a folder on desktop (Can also be done in R-


studio clicking Session Menu button and selecting working directory)
rm(file) Deletes file
help(“setwd”) Help on specific function like the setwd command
savehistory(“filename”) saves the history
save.image(“filename”) saves the workspace
load(“workspace”) loads the workspace
q() quits R
logdata<-log2(data) This creates a new data matrix called with log2data converted from data

Once you have installed R and R Studio you will need to install BioConductor packages using the
biocLite.R installation script.

In an R command window, type the following:

> if (!requireNamespace("BiocManager", quietly = TRUE))


install.packages("BiocManager") #install BiocManager to begin downloading other packages
> BiocManager::install(version = "3.12") #make sure the version of biocManager is the correct version for R
version consult the bioconductor website to see which R version uses which BiocManager version
> BiocManager::install(c("affy","multtest","hopach","affyQCReport","genefilter")) #now install packages
> install.packages(c("dplyr","tibble","gplots","readr")) #these are not from bioconuctor but instead CRAN

This installs the following packages: affy, affydata, affyPLM, annaffy, annotate, Biobase, Biostrings, DynDoc,
gcrma, genefilter, geneplotter, hgu95av2.db, limma, marray, matchprobes, multtest, ROC, vsn, xtable,
affyQCReport. After downloading and installing these packages, the script prints "Installation complete" and
TRUE

Functions:

Importing a data matrix

> data<-read.table(“filename in tab separated format.txt”,sep=”\t”,as.is=T,header=T,row.names=1)


#note that this will set the first column of data as rownames and these must all be unique values i.e. Tidy
data

Determining the dimensions of the dataframe, returns size of the data frame by rows and columns

> dim(data)

Getting the header names of the variables in the data

> names(data)
ALS411
Biomedical Data Systems and Informatics

Seeing the structure of the data, very useful for accessing the information in the data

> str(data)

Seeing a breakdown (min, max, 1st quantile, distributsions ect… of the data

> summary(data)

Log2 transforming the data and create a new data matrix called “log2data”

> logdata<-log2(data)

Make a scatter plot of one of the control and exercise legs

> plot(logdata[,1],logdata[,2])

Pull out the 8 am data from the data matrix and put it in a new matrix called 8am

> amdata<-logdata[,1:8]

Make a scatter plot of all possible combinations of amdata

> pairs(amdata)

Exporting Log2 transformed Data to a tab separated file

> write.table(log2data,"log2data.txt",sep="\t")

Data Types Determination and Conversion


Type conversions in R work as you would expect. For example, adding a character string to a
numeric vector converts all the elements in the vector to character.

Use is.data to test for data type data. Returns TRUE or FALSE
is.numeric(), is.character(), is.vector(), is.matrix(), is.data.frame()as.numeric(),

Use as.data to coerce the data from one type to another.


as.character(), as.vector(), as.matrix(), as.data.frame()

Exporting data from R into a tab delimited table

> write.table(tablename,"tablename.txt",sep="\t")

Here is how to sort and check by just looking at the first or last five rows of data

head(logdata[order(row.names(logdata)),])

tail(logdata[order(row.names(logdata)),])

You might also like