Professional Documents
Culture Documents
of rows and
columns
length(mydata) # Total observations
head(mydata) # Display mydata's column
headers and first 6 rows
head(mydata, n=10) # Display mydata's column
headers
or
and first 10 rows
head(mydata, 10)
describe(mydata)
Recode Variables
# Recode using ifelse function
ifelse(mydata$age<=15, ”small”, ”large”)
# Combining several comparisons with logical operators
ifelse((mydata$age<=15 &
mydata$gender=="female"), "small", "large")
# Coding in to several steps (e.g. age into groups)
data$AgeGroup <- cut(data$age, c(1, 15, 30, 100), c("Below 15", "15-
29", "30 or Above")) #Where, 1= lower limit and 100= upper limit
Subsetting
mean(age[gender=="female"])# = is used to assign object and == is
used to represent the meaning of equality
Export R Data
Labelling Values
# variable v1 is coded 1, 2 and 3
# we want to attach value labels 1=Yes, 2=No, 3=Don't Know
q() # Exits R
R Operators
+ Add, 2 + 3 = 5
- Subtract, 5 - 2 = 3
* Multiply, 2 * 3 = 6
/ Divide, 6 / 2 = 3
^ Exponent, 2 ^ 3 = 8
%% Modulus operator, 9%%2 = 1
%/% Integer division, 9 %/% 2 = 4
< Less than
> Greater than
= Equal to
<= Less than or equal to
>= Greater than or equal to
!= Not equal to
! Not
| OR
& And
Pie Chart
Histogram
Error Bars
High-Low
Scattered Plot
Line graph
Population Pyramid
Box Plot
ROC curve
Statistics
summary(mydata) # Extract a basic statistics
install.packages("psych") # To extract elaborated statistical
summary
library(psych)
describe(mydata)
Frequency Tables
Crosstabs
library(MASS)
t-test:
a) one-sample
b) paired
c) independent sample
MANOVA
Correlation