0% found this document useful (0 votes)
0 views4 pages

Useful R Functions-1

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

Some

 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("Psychometrics", dependencies = T) # Install packages for


psychological data analysis.

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

# R will NOT READ things written after # sign


#square and square roots

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

Most basic operator in R are as follows


** Exponentiation
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== exactly equal to
!= not equal to
!x not x
x | y x OR y
x & y x AND Y
Is TRUE(x) Tests if x is True

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)

y<-matrix(1:30, nrow=6,ncol=5) # y is a matrix


# another matrix 2 × 2
cells <- c(20,40,14,18)
rnames <- c("R1", "R2")
cnames <- c("C1", "C2")
mymatrix <- matrix(cells, nrow=2,ncol = 2, byrow = TRUE, dimnames= list
(rnames, cnames))

Read Data
mydata <- read.table (“path”, sep=“,”, header=TRUE)

# for mac useres


data<- read.csv("/Users/macbook/Desktop/SEM/trial.csv" , header = T)

# for windows users


data<- read.csv("C:/trail.csv" , header = T)

In case of the data in the .txt format


read.txt()

easier way to get the data is to use following code

read.table(file.choose(),sep=“,”,header=TRUE) # open csv with dialogue box

Writing  your  function  

subtract = function(a, b)
{ result = a - b
return(result) }
subtract(10,6)

Describing  Data  
library(Hmisc)
describe(mydata)

Correlations  and  covariances  among  variables    

cor(x,y)

cor(x,y, method = “spearman”)

cor(mtcars, use="complete.obs", method="kendall") # data from mtcars. Use


only complete observations.

cov(mtcars, use="complete.obs")

cor.test(x,y, method = “pearson”)

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

one  sample  t-­‐test    


t.test(x, mu=10) # Ho: mu=10

#independent  samples  Wilcox  sum  rank  test    


wilcox.test(y~x) # where y is DV and x is a factor

#  dependent  samples  Wilcoxon  Signed  Rank  Test      


wilcox.test(x, y ,paired = TRUE) # where x and y are numeric DV

Randomized  Block  Design  -­‐  Friedman  Test      


friedman.test(y~x|a)# where y are the data values, x is a grouping variable
and A is a blocking factor

#  Multiple  Linear  Regression    


fit <- lm(y ~ x1 + x2, data=mydata)# y is DV, x1 and x2 are IV
summary(fit) # show results

#  Other  useful  functions      


coefficients(fit) # provides coefficients
confint(fit, level=0.99) # confidence interval for parameters
anova(fit) # anova summary table
fitted(fit) # predicted valuesby model
residuals(fit) # residualsvalues
vcov(fit) # covariance matrix among model parameters
influence(fit) # regression diagnostics statistics

#  One  Way  Anova  (Completely  Randomized  Design)    


fit <- aov(y ~ x) # y is DV and x is IV

#  Randomized  Block  Design      


fit <- aov(y ~ x + a) # y is DV, x is IV and a is blocking factor

#  Two-­‐Way  Factorial  Design  Independent  samples    


fit <- aov(y ~ A + B + A:B)# y is DV, A and B are two IV’s

#  One  Within  Factor    


fit <- aov(y~A+Error(Subject/A)) # y is DV, A is IV and Subject is
variable denoting subjects

#  MANOVA  with  four  Dependent  Variables.      

Y <- data.frame(y1, y2, y3, y4)

fit <- manova(Y ~ A)summary(fit, test="Wilks") # Y is data.frame of DV’s


and A is IV

logistics  regression  

fit <- glm(y~x, family = binomial()) # y is dichotomous DV and X is IV

Read More at:

Belhekar, V. M. (2016). Statistics for Psychology using R. Sage Publication,


New Delhi.

You might also like