STATS LAB Basics of R PDF
STATS LAB Basics of R PDF
EMBEDDED LAB
R-BASICS-LAB-I & LAB-2
Output:-
# : The character # marks the beginning of a
comment.All characters until the end of the line are
ignored.
> # mu is the mean
> # x <- 20 is treated as comment only
Capital and small letters are different.
The command c(1,2,3,4,5) combines the numbers 1,2,3,4
and 5 to a vector.
R as a calculator
Integer Division: Division in which the fractional part
(remainder) is discarded
Interpretation
If x < 6 (TRUE), then x = x2 (YES) .
If x ≥ 6 (FALSE), then x = x + 1 (NO).
The for loop
If the number of repetitions is known in advance a for()
loop can be used.
The while() loop
If the number of loops is not known in before, e.g. when
an iterative algorithm to maximize a likelihood function
is used, one can use a while() loop.
The repeat loop
The repeat loop doesn’t test any condition — in contrast
to the while() loop — before entering the loop and also
not during the execution of the loop.
Data Frames
In a data frame, we can combine variables of equal
length,with each row in the data frame containing
observations on the same unit.
Data frames contain complete data sets that are mostly
created with other programs (spreadsheet-files, software
SPSS-files, Excel-files etc.).
Variables in a data frame may be numeric (numbers) or
categorical (characters or factors).
Package ―MASS‖ describes functions and datasets to support
Venables and Ripley, ``Modern Applied Statistics with S'' (4th
edition 2002)
Structure of the data:
The result of str gives the dimension as well as the name
and type of each variable.
Extract a variable from data frame using $
Suppose we want to extract information on variable School from the
data set painters.
Subsets of a data frame can be obtained with subset() or
with the second equivalent command:
The command split partitions the data set by values of a
specific variable. This should preferably be a factor
variable.
Practice
1. x=23, y=x^2, z=y^3+x^2
2. 2+3-4*6/3-2^3/4+7-6*2**3/2+2-4+5 ?
3. -478/365-12^3/47-6**2**3-27^-(1/3)-7^6*3
4. c(1,2,3,4)*c(1,2,2,4)^c(2,1,3,2)**c(1,2,3,3)-
c(2,3,402653185,262145) ?
5. c(2,3,4,5)^c(2,3)+c(12,23,14,25)^c(3,2)-c(5,6,7,8)*c(2,3)?
max(c(62,83,44,75)^-c(9,-3)) / min(c(52,62,71,85) ^
c(2,3) ) -prod(c(1,2,1,2) ^c(1,2)) +max(c(12,13,14,15) ^
c(2,3)) ?
X<-matrix(nrow=3,ncol=3,data=
c(10,20,30,40,50,60,70,80,90),byrow=F) then X[ ,2]?
sqrt(abs(seq(-6,6, by = 3)))?
letter[1:3]?
LETTERS[18:12]
rep(2:5,4)
rep(70:65, times=3)?
rep(20:25, each=3)?
x <- c(10, 75, 20, 35, 30, 40, 180, 50, 60, 27, 70, 67, 80, 50,
39, 120)
>x[(x>50)]?
>x[(x - 20 > 40)]
>x[(x^2 + 10 > 50)]