This repository has been archived by the owner on Jan 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6895695
commit 7b5f6fd
Showing
7 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |