Useful R Functions-1
Useful R Functions-1
Useful R Functions-1
useful
R
functions
Vivek
Belhekar
Ph.D.
University
of
Mumbai
vivek.belhekar@gmail.com
Belhekar,
V.
M.
(2016).
Statistics
for
Psychology
using
R.
Sage
Publication.
Start
R
and
Type
following
in
R
and
press
enter.
Keep
computer
connected
to
the
internet.
It
will
install
various
R
packages
that
are
useful
to
us.
This
may
take
half
and
hour
to
an
hour
depending
on
your
computer
and
internet
speed.
after
running
first
line,
R
will
ask
you
to
choose
can
mirror
you
can
choose
any
one.
install.packages("ctv")
library("ctv")
install.views("Econometrics", dependencies = T)
install.views("Distributions", dependencies = T)
install.views("ExperimentalDesign", dependencies = T)
install.views("Graphics", dependencies = T)
install.views("Multivariate", dependencies = T)
install.views("Robust", dependencies = T)
install.views("SocialSciences", dependencies = T)
install.views("gR", dependencies = T)
R as calculator
x <- 10
y <- 5
z <- x + y
z1 <- x - y
z2 <- x/y
z3 <- x * y
x^2 # square of x
sqrt(9) # square-root of 9
log(x) # log of x
exp(1) # exponential of 1
abs(x) # absolute value of x
#sin(x); cos(x); tan(x); asin(x); acos(x); atan(x) are other useful
functions
Vectors
a <- c(1, 2, 2, 3, 4, 5.4, -3, 11) # numeric vector
b <- c("one", "two", "three") # character vector
c <- c(FALSE, TRUE, TRUE, FALSE, TRUE, FALSE) #logical vector
mydata <- data.frame (a, b, c)
mydata <- cbind (a, b, c)
# factor as a object
gender <- c(rep("male",30), rep("female", 40))
gender <- factor(gender)
Read Data
mydata <- read.table (“path”, sep=“,”, header=TRUE)
subtract = function(a, b)
{ result = a - b
return(result) }
subtract(10,6)
Describing
Data
library(Hmisc)
describe(mydata)
cor(x,y)
cov(mtcars, use="complete.obs")
t-‐test
#
independent
samples
t-‐test
t.test(y~x) # where y is DV and x is a factor
#
paired
t-‐test
t.test(x,y, paired=TRUE) # where x and y are dependent
logistics regression