R language
Basic R commands
Hi folks, Today we going to learn about R Language and its basic commands.
R is a programming language and free software environment for statistical computing and graphics that is supported by the R Foundation for Statistical Computing. The R language is widely used among statisticians and data miners for developing statistical software and data analysis.
Write these small script into rscript (New File -> Rscript).After write every script hit ctrl+Enter then your script will be run.
R is a programming language and free software environment for statistical computing and graphics that is supported by the R Foundation for Statistical Computing. The R language is widely used among statisticians and data miners for developing statistical software and data analysis.
Write these small script into rscript (New File -> Rscript).After write every script hit ctrl+Enter then your script will be run.
1+1
output will show in console window like this> 1+1
[1] 2
Command | Output |
---|---|
1-1 | > 1-1 [1] 0 |
6/2 | > 6/2 [1] 3 |
pi^2 | > pi^2 [1] 9.869604 |
exp(1) | > exp(1) [1] 2.718282 |
var1 <- pi | > var1 <- pi |
var1^2 | > var1^2 [1] 9.869604 |
var2 = 42 | > var2 = 42 |
exp(3) -> var3 | > exp(3) -> var3 > var3 [1] 20.08554 |
Defining Vector | |
vec1 <- c(1,2,3) | > vec1 <- c(1,2,3) > vec1 [1] 1 2 3 |
vec2 <- 1:11 | > vec2 <- 1:11 > vec2 [1] 1 2 3 4 5 6 7 8 9 10 11 |
vec3 <- rep(x=4,7) (rep stands for repeat some certain number) | > vec3 <- rep(x=4,7) > vec3 [1] 4 4 4 4 4 4 4 |
vec4 <- seq(from=1,to=12,by=1.333) (sequence number vector. by stands for increment by that value) | > vec4 <- seq(from=1,to=12,by=1.333) > vec4 [1] 1.000 2.333 3.666 4.999 6.332 7.665 8.998 10.331 11.664 |
vec5 <- seq(1,12,length.out =10 ) | > vec5 <- seq(1,12,length.out =10 ) > vec5 [1] 1.000000 2.222222 3.444444 4.666667 5.888889 7.111111 8.333333 9.555556 10.777778 [10] 12.000000 |
Indexing Vector(Same kind of element) | |
vec4[3] (Access 3rd element of vec4) | > vec4[3] [1] 3.666 |
vec4[1:3] (Access to st to 3rd element) | > vec4[1:3] [1] 1.000 2.333 3.666 |
vec4[23:24] | > vec4[23:24] [1] NA NA |
vec4[c(1,3,7)] (Access specific index element ) | > vec4[c(1,3,7)] [1] 1.000 3.666 8.998 |
vec4[c(1,1,1,2)] | > vec4[c(1,1,1,2)] [1] 1.000 1.000 1.000 2.333 |
vec4[-4] (access except element index number 4) | > vec4[-4] [1] 1.000 2.333 3.666 6.332 7.665 8.998 10.331 11.664 |
vec4[-(1:3)] (access except index number 1 to 3) | > vec4[-(1:3)] [1] 4.999 6.332 7.665 8.998 10.331 11.664 |
vec4[-c(4,5,7)] (access except index number 4 ,5 and 7) | > vec4[-c(4,5,7)] [1] 1.000 2.333 3.666 7.665 10.331 11.664 |
vec4[9:4] (Access element reverse order) | > vec4[9:4] [1] 11.664 10.331 8.998 7.665 6.332 4.999 |
Relational Operator | |
vec5 <- c(2,2,2) (Defining vector) | > vec5 <- c(2,2,2) > vec5 [1] 2 2 2 |
all.equal(vec5,c(2,1,2)) | > all.equal(vec5,c(2,1,2)) [1] "Mean relative difference: 0.5" |
all.equal(vec5,c(2,2,2)) | > all.equal(vec5,c(2,2,2)) [1] TRUE |
vec5==c(2,2,2) | > vec5==c(2,2,2) [1] TRUE TRUE TRUE |
vec5>c(2,2,2) | > vec5>c(2,2,2) [1] FALSE FALSE FALSE |
vec5>c(1,2,3) | > vec5>c(1,2,3) [1] TRUE FALSE FALSE |
vec5<=c(1,2,3) | > vec5<=c(1,2,3) [1] FALSE TRUE TRUE |
vec5!=c(1,2,3) | > vec5!=c(1,2,3) [1] TRUE FALSE TRUE |
List Operator(It can be anything) | |
list1 <- list(vec1,vec2,vec4) | > list1 <- list(vec1,vec2,vec4) > list1 [[1]] [1] 1 2 3 [[2]] [1] 1 2 3 4 5 6 7 8 9 10 11 [[3]] [1] 1.000 2.333 3.666 4.999 6.332 7.665 8.998 10.331 11.664 |
list2 <-list(booyah=vec1,boombody=vec2,bam=vec3) | > list2 <-list(booyah=vec1,boombody=vec2,bam=vec3) > list2 $booyah [1] 1 2 3 $boombody [1] 1 2 3 4 5 6 7 8 9 10 11 $bam [1] 4 4 4 4 4 4 4 |
list2$booyah ($ sign stands foraccessing name) | > list2$booyah [1] 1 2 3 |
listvar <- list2$bam (Redefine a list) | > listvar <- list2$bam > listvar [1] 4 4 4 4 4 4 4 |
list2$bam | > list2$bam [1] 4 4 4 4 4 4 4 |
listvar <-list2$bam[1:2] | > listvar <-list2$bam[1:2] > listvar [1] 4 4 |
Comments