You are on page 1of 21

R Basics

Installing R
The current version can be downloaded from
http://cran.r-project.org
Download the base file from this site

R Studio (IDE for R)


R Studio is Integrated Development Environment for R,
which combines everything in single place.
Can be downloaded from
https://www.rstudio.com/products/rstudio/download2/

Setting working directory


Find current working directory
getwd()
Eg:
> getwd()
[1] "C:/Users/PrabinRaj/Documents"
Setting working directory
setwd()
eg:
setwd("C:/Users/PrabinRaj/Documents/R")
*note: R uses / different from windows \

Getting Help

From R environment
help.start()
# general help
help(foo)
# help about functionfoo
?foo
# same thing
apropos("foo") # list all functions containing string foo
example(foo) # show an example of functionfoo

In internet

http://search.r-project.org/
http://rseek.org/ (custom google for R)
R mailing list (https://www.r-project.org/mail.html)
Stack overflow (R is very popular Tag)
And other hundreds of tutorial sites, free books and blogs
https://thebook.io/006723/ch01/01/ (book in korean)

Input Data

Input Data: Basic Arithmetic


R can be used as calculator by typing inputs next to
prompt (>)
> 2+1
[1] 3
Functions available:
List of mathematical Annotation in R is available at:
http://astrostatistics.psu.edu/su07/R/html/grDevices/html
/plotmath.html

Input Data :Assignment


<- symbol is used to assignment
A<- 2 # variable A is assigned with value 2
B<-c(1,2,3) # variable B is assigned with value 1, 2, 3,
Here c stands for combine

Reading CSV file


read.csv(file = , header = TRUE, sep = , )
e.g
Survey<- read.csv(~/R/D1Survey.csv, header = T, sep = , )
Task:
1. Download file from
https://drive.google.com/file/d/0B27hSDyza01qYUJqTVhxcjRmVTg/view?usp=sharing
2. Or Use registration_prepostTestV2.csv in Nepal Ultrasound/ Data/Analysis
usData<-read.csv(file = ~/Prabin/Nepal Ultrasound/Data/Analysis/registration_prepostTestV2.csv,
header=T, sep = ,)
To know more about read.csv
Help(read.csv)
:D

Input Data : From other software


format
Other Software like : excel, SPSS, STATA, SAS

specific packages are available for getting data and syntax depends on
packages
Eg:
Foreign package : read and write data from some version of EPI Info, SAS, SPSS,
STATA, Minitab, Weka and dBase
For excel: Xlconnect, [loadWorkbook () ; readWorksheet()]
SPSS: read.spss ()
STATA : package readstata13 ; read.dta13(path to file.dta)
Visit: https://cran.r-project.org/web/packages/readstata13/README.html
All the packages have documentation hosted in R CRAN server

Explore the data sets


summary(usData)
str(usData)
names(usData)
head(usData)
usData$ID
usData[1]
usData[,1]
usData[1,]

Basic Type of Data


Variable Type
1. Numbers
2. Strings
3. Factors
4. Data Frames
5. Logical

Graphics : ggplot2 (package)


The Building Blocks
data
aesthetic mapping
geometric object
statistical transformations
scales
coordinate system
position adjustments
faceting

ggplot(data = <default data set>,


aes(x = <default x axis variable>,
y = <default y axis variable>,
... <other default aesthetic mappings>),
... <other plot defaults>) +
geom_<geom type>(aes(size = <size
variable for this geom>,
... <other aesthetic mappings>),
data = <data for this point geom>,
stat = <statistic string or function>,
position = <position string or

scale_<aesthetic>_<type>(nam
e = <"scale label">,
breaks = <where to
put tick marks>,
labels = <labels for
tick marks>,
... <other options
for the scale>) +

function>,
color = <"fixed color
specification">,
<other arguments, possibly passed
to the _stat_ function) +

theme(plot.background =
element_rect(fill = "gray"),
... <other theme
elements>)

QGIS

Basic file types


Shape File
Point File (Geo Points)
Data Files

Importing Shape File / Geocode from


google map (KML file )
Option I :
Easiest way Drag and Drop
Option II:
Add layer Vector layer
Note: there are some standard systems for mapping , Nepals
shape files uses WGS 84 UTM ZONE 45 N EPSG:32645 (mostly,
which is little bit different than GPS co-ordinate system)

CSV Data file


QGIS doesn't support excel file directly, so its suggested
to get the data CSV file or dBase
From layer >> Add layer >> Add delimited text layer

Getting Data from file into map file


By join
Right click on the Map
Click on properties
From properties Tab click on Join
And follow the instruction

You might also like