Skip to content
This repository has been archived by the owner on Jan 1, 2019. It is now read-only.

Commit

Permalink
Basics on Working with R
Browse files Browse the repository at this point in the history
  • Loading branch information
vivekagal1998 committed Oct 2, 2017
1 parent 6895695 commit 7b5f6fd
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 0 deletions.
10 changes: 10 additions & 0 deletions R/Input nd Output.R
Original file line number Diff line number Diff line change
@@ -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."))
18 changes: 18 additions & 0 deletions R/for_loop.R
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions R/for_loop1.R
Original file line number Diff line number Diff line change
@@ -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])
}
10 changes: 10 additions & 0 deletions R/if_else.R
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions R/sort_inbuilt.R
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions R/switch_case.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
gk <- switch (
2,
"First",
"Second",
"Third",
"Fourth"
)
print (gk)
## Prints "Second"
14 changes: 14 additions & 0 deletions R/while.R
Original file line number Diff line number Diff line change
@@ -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());
}

0 comments on commit 7b5f6fd

Please sign in to comment.