From 7b5f6fd5353fd60442e56241ede865efe2a8ad91 Mon Sep 17 00:00:00 2001 From: Vivek Shah Date: Mon, 2 Oct 2017 13:22:54 +0530 Subject: [PATCH] Basics on Working with R --- R/Input nd Output.R | 10 ++++++++++ R/for_loop.R | 18 ++++++++++++++++++ R/for_loop1.R | 12 ++++++++++++ R/if_else.R | 10 ++++++++++ R/sort_inbuilt.R | 17 +++++++++++++++++ R/switch_case.R | 9 +++++++++ R/while.R | 14 ++++++++++++++ 7 files changed, 90 insertions(+) create mode 100644 R/Input nd Output.R create mode 100644 R/for_loop.R create mode 100644 R/for_loop1.R create mode 100644 R/if_else.R create mode 100644 R/sort_inbuilt.R create mode 100644 R/switch_case.R create mode 100644 R/while.R diff --git a/R/Input nd Output.R b/R/Input nd Output.R new file mode 100644 index 0000000..b07c964 --- /dev/null +++ b/R/Input nd Output.R @@ -0,0 +1,10 @@ +##readLine is used to take I/P from user + +my.name <- readline(prompt="Enter name: ") +my.age <- readline(prompt="Enter age: ") + +# convert character into integer using as.integer +my.age <- as.integer(my.age) + +## Printing Out in the Screen +print(paste("Hi,", my.name, "next year you will be", my.age+1, "years old.")) diff --git a/R/for_loop.R b/R/for_loop.R new file mode 100644 index 0000000..8b166c6 --- /dev/null +++ b/R/for_loop.R @@ -0,0 +1,18 @@ + # Create a vector filled with random normal +u1 <- rnorm(30) +print("This loop calculates the square of the first 10 elements of vector u1") + +# Initialize `usq` + +usq <- 0 + + +## using for loop to iterate over first 10 elements in ul + +for(i in 1:10) { + # i-th element of `u1` squared into `i`-th position of `usq` + usq[i] <- u1[i]*u1[i] + print(usq[i]) +} + +print(i) \ No newline at end of file diff --git a/R/for_loop1.R b/R/for_loop1.R new file mode 100644 index 0000000..484e056 --- /dev/null +++ b/R/for_loop1.R @@ -0,0 +1,12 @@ +# making vector prime +primes <- c(2, 3, 5, 7, 11, 13) + +# loop version 1 +for (p in primes) { + print(p) +} + +# loop version 2 +for (i in 1:length(primes)) { + print(primes[i]) +} \ No newline at end of file diff --git a/R/if_else.R b/R/if_else.R new file mode 100644 index 0000000..92ae19a --- /dev/null +++ b/R/if_else.R @@ -0,0 +1,10 @@ +if (TRUE){ + print ("This will execute...") +} + +else{ + print ("but this will not.") +} +## This will execute... + +##similarly if else - if can be coded \ No newline at end of file diff --git a/R/sort_inbuilt.R b/R/sort_inbuilt.R new file mode 100644 index 0000000..c48375d --- /dev/null +++ b/R/sort_inbuilt.R @@ -0,0 +1,17 @@ +# x is a vector on which various Operations are to be performed + +x +## 7 1 8 3 2 6 5 2 2 4 + +# sort in ascending order +sort(x) +## 1 2 2 2 3 4 5 6 7 8 + +# sort in descending order +sort(x, decreasing=TRUE) + +## 8 7 6 5 4 3 2 2 2 1 + +# vector x remains unaffected +x +# 7 1 8 3 2 6 5 2 2 4 \ No newline at end of file diff --git a/R/switch_case.R b/R/switch_case.R new file mode 100644 index 0000000..b8b6745 --- /dev/null +++ b/R/switch_case.R @@ -0,0 +1,9 @@ +gk <- switch ( +2, +"First", +"Second", +"Third", +"Fourth" +) +print (gk) +## Prints "Second" \ No newline at end of file diff --git a/R/while.R b/R/while.R new file mode 100644 index 0000000..c9a8143 --- /dev/null +++ b/R/while.R @@ -0,0 +1,14 @@ +# Your User Defined Function + +readinteger <- function(){ + n <- readline(prompt="Please, enter your ANSWER: ") +} + +##converts response to integer from character + +response <- as.integer(readinteger()) + +while (response!=42) { + print("Sorry, the answer to whatever the question MUST be 42"); + response <- as.integer(readinteger()); +} \ No newline at end of file