Welcome to the Simplilearn Community

Want to join the rest of our members? Sign up right away!

Sign Up

Data Science Certification Training - R Programming | JAN 11 - FEB 09 | Samridhi

Samridhi Dutta

Well-Known Member
Alumni
Trainer
#Assignment: print a table of 2: 10 using nested for loops
"2 X 1 = 2"
"2 X 2 = 4"
"2 X 3 = 6"

"3 X 1 = 3"
"3 X 2 = 6"
 

Akshay_15

New Member
#tables from 2 to 10

for(i in 2:10){
cat("\n","Table for ",i,"\n",sep="")
for(j in 1:10){
cat(i,"X",j,"=",i*j,"\n", sep=" ")
}
}
 

Kousik Das

New Member
#Assignment: print a table of 2: 10 using nested for loops
"2 X 1 = 2"
"2 X 2 = 4"
"2 X 3 = 6"

"3 X 1 = 3"
"3 X 2 = 6"

#========================================
# assign value of num = 2 or any integere number
num = 2
# use for loop to iterate 10 times
for(i in 1:10) {
print(paste(num,'x', i, '=', num*i))
}
 
Hi Samridhi,

I have a doubt regarding the use-case of "paste" and "cat" command.In the below code can i use cat command instead of paste?

p = NULL #neither bool nor integer nor numeric nor character
for(i in 1:10)
{
if(i==1)
p = i
else
p = paste(p,i, sep = ",")
}
p
print(p)

Thanks,
Aaditya
 

Gautam Lakshmiratan

Active Member
Need to understand why this trailing message comes after every iteration. The output is:
"30 Levels: Akira American Beauty Back to the Future ... Whiplash"
The code is:
# Creating a start point.
iteration = 1

# want to repeat until we reach the sixth operation -- but not execute the sixth time.
# While iteration is less or equal to five...
while (iteration <= 5) {

print(c("This is iteration number:",as.character(iteration)))

# ...print the "name" column of the iteration row.
print(movies_data[iteration,]$name)

# And then, we increase the "iteration" value -- so that we actually reach our stopping condition
iteration = iteration + 1
}

Dataset used Movie_Data
source: download.file("https://ibm.box.com/shared/static/n5ay5qadfe7e1nnsv5s01oe1x62mq51j.csv", destfile="movies-db.csv")
 
Last edited:
Hi Samridhi,
I am practicing the code that we discussed last week. Regarding the for loop, I could execute the code shown below initially. But later, when I was trying to use break command and execute, then, the output shown is the same code and the variable itself and not the expected output. Not sure, where I am missing. Then onward, none of the codes are executing with Ctrl+Enter.

for(day in 1:31)
{
if(day>=20 & day<28)
next
cat("The child came on the date. The date is: ", day,"/01/2020\n", sep="")
if(day==29)
{
cat("The Child is leaving the School on: ", day,"01/2020", sep = " ")
break
}
}
day

Output shown in console as below:
+ for(day in 1:31)
+ { + if(day>=20 & day<28)+ next+ cat("The child came on the date. The date is: ", day,"/01/2020\n", sep="")+ if(day==29)+ {+ cat("The Child is leaving the School on: ", day,"01/2020", sep = " ")+ break+ }+ }+ day
Can I get help to know what is going wrong with the code?
 
Last edited:
Dear Samridhi, can you please check below two questions?
class(cat(1,2,3)) #why NULL?
class(paste(1,2,3)) #why character?

cat() prints and does not return a value and paste() returns character vector

putting a code snippet which explains this

>var = cat("name")
name
> var1 = paste("name")
>
> var
NULL
> var1
[1] "name"
>
 

John_302

Member
Hi Samridhi,
I am practicing the code that we discussed last week. Regarding the for loop, I could execute the code shown below initially. But later, when I was trying to use break command and execute, then, the output shown is the same code and the variable itself and not the expected output. Not sure, where I am missing. Then onward, none of the codes are executing with Ctrl+Enter.

for(day in 1:31)
{
if(day>=20 & day<28)
next
cat("The child came on the date. The date is: ", day,"/01/2020\n", sep="")
if(day==29)
{
cat("The Child is leaving the School on: ", day,"01/2020", sep = " ")
break
}
}
day

Output shown in console as below:
+ for(day in 1:31)
+ { + if(day>=20 & day<28)+ next+ cat("The child came on the date. The date is: ", day,"/01/2020\n", sep="")+ if(day==29)+ {+ cat("The Child is leaving the School on: ", day,"01/2020", sep = " ")+ break+ }+ }+ day
Can I get help to know what is going wrong with the code?

Hi believe if you scroll down the console window, you should find the output immediately after the print of the code
 

Gautam Lakshmiratan

Active Member
#Assignment: print a table of 2: 10 using nested for loops
"2 X 1 = 2"
"2 X 2 = 4"
"2 X 3 = 6"

"3 X 1 = 3"
"3 X 2 = 6"
Please find results below..

[1] "2 X 1 = 2"
[1] "2 X 2 = 4"
[1] "2 X 3 = 6"
[1] "2 X 4 = 8"
[1] "2 X 5 = 10"
[1] "2 X 6 = 12"
[1] "2 X 7 = 14"
[1] "2 X 8 = 16"
[1] "2 X 9 = 18"
[1] "2 X 10 = 20"

[1] "3 X 1 = 3"
[1] "3 X 2 = 6"
[1] "3 X 3 = 9"
[1] "3 X 4 = 12"
[1] "3 X 5 = 15"
[1] "3 X 6 = 18"
[1] "3 X 7 = 21"
[1] "3 X 8 = 24"
[1] "3 X 9 = 27"
[1] "3 X 10 = 30"

[1] "4 X 1 = 4"
[1] "4 X 2 = 8"
[1] "4 X 3 = 12"
[1] "4 X 4 = 16"
[1] "4 X 5 = 20"
[1] "4 X 6 = 24"
[1] "4 X 7 = 28"
[1] "4 X 8 = 32"
[1] "4 X 9 = 36"
[1] "4 X 10 = 40"

[1] "5 X 1 = 5"
[1] "5 X 2 = 10"
[1] "5 X 3 = 15"
[1] "5 X 4 = 20"
[1] "5 X 5 = 25"
[1] "5 X 6 = 30"
[1] "5 X 7 = 35"
[1] "5 X 8 = 40"
[1] "5 X 9 = 45"
[1] "5 X 10 = 50"
 

Attachments

  • Assignment_1.txt
    102 bytes · Views: 1
Hi Samridhi,
I am practicing the code that we discussed last week. Regarding the for loop, I could execute the code shown below initially. But later, when I was trying to use break command and execute, then, the output shown is the same code and the variable itself and not the expected output. Not sure, where I am missing. Then onward, none of the codes are executing with Ctrl+Enter.

for(day in 1:31)
{
if(day>=20 & day<28)
next
cat("The child came on the date. The date is: ", day,"/01/2020\n", sep="")
if(day==29)
{
cat("The Child is leaving the School on: ", day,"01/2020", sep = " ")
break
}
}
day

Output shown in console as below:
+ for(day in 1:31)
+ { + if(day>=20 & day<28)+ next+ cat("The child came on the date. The date is: ", day,"/01/2020\n", sep="")+ if(day==29)+ {+ cat("The Child is leaving the School on: ", day,"01/2020", sep = " ")+ break+ }+ }+ day
Can I get help to know what is going wrong with the code?
Issue is resolved by support team. Thanks
 
Hi Samridhi,

I could finally complete coding. below is my code. Sorry for late submission of the assignment.

for (i in 2:10)
{
for(j in 1:10)
{
print(paste(i,"*",j,"=",i*j,sep=""))
}
}

Enjoying coding. Thanks
 
Hi Samridhi,
I was trying for loop.....as per our class. Not sure where i went wrong in codes....desired results are not coming


for(i in 1:31)
{
if (i>=18 & i<=20)
next
cat("Child came on this date:",i,"08/2019\n",sep=" ")
if(i==29)
{
cat("child left the school:",i"/08/2019\n",sep=" ")
break
}
}


 

Gautam Lakshmiratan

Active Member
Need to understand why this trailing message comes after every iteration. The output is:
"30 Levels: Akira American Beauty Back to the Future ... Whiplash"
The code is:
# Creating a start point.
iteration = 1

# want to repeat until we reach the sixth operation -- but not execute the sixth time.
# While iteration is less or equal to five...
while (iteration <= 5) {

print(c("This is iteration number:",as.character(iteration)))

# ...print the "name" column of the iteration row.
print(movies_data[iteration,]$name)

# And then, we increase the "iteration" value -- so that we actually reach our stopping condition
iteration = iteration + 1
}

Dataset used Movie_Data
source: download.file("https://ibm.box.com/shared/static/n5ay5qadfe7e1nnsv5s01oe1x62mq51j.csv", destfile="movies-db.csv")
-----------------------
could be this happening because of using "stringsAsFactors=TRUE" ? please help me to understand this!
 
Hi Samridhi,

In Class 3 you asked to find the Age of "Moran, Mr. James" from the titanic dataset.

I am unable to get the solution for the same by using grep function....please help.

Solution: grep(pattern = "Moran, Mr. James",x=tit$Age,fixed = T,value = T)

output: character(0)

please help
 
Hi Samridhi,

In Class 3 you asked to find the Age of "Moran, Mr. James" from the titanic dataset.

I am unable to get the solution for the same by using grep function....please help.

Solution: grep(pattern = "Moran, Mr. James",x=tit$Age,fixed = T,value = T)

output: character(0)

I have found out another technique, closest to the answer....

1) . tit[c(6),]
output:

PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare
6 6 0 3 Moran, Mr. James male 29.68985 0 0 330877 8.4583
Cabin Embarked_S Embarked_Q Is_Female
6 No Cabin


2)
filter <- tit$Name == "Moran, Mr. James"
tit[filter, ]

output:
PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare
6 6 0 3 Moran, Mr. James male 29.68985 0 0 330877 8.4583
Cabin Embarked_S Embarked_Q Is_Female
6 No Cabin 0 1 0


please help
 
Hi Samridhi,

In Class 3 you asked to find the Age of "Moran, Mr. James" from the titanic dataset.

I am unable to get the solution for the same by using grep function....please help.

Solution: grep(pattern = "Moran, Mr. James",x=tit$Age,fixed = T,value = T)

output: character(0)

I have found out another technique, closest to the answer....

1) . tit[c(6),]
output:

PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare
6 6 0 3 Moran, Mr. James male 29.68985 0 0 330877 8.4583
Cabin Embarked_S Embarked_Q Is_Female
6 No Cabin


2)
filter <- tit$Name == "Moran, Mr. James"
tit[filter, ]

output:
PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare
6 6 0 3 Moran, Mr. James male 29.68985 0 0 330877 8.4583
Cabin Embarked_S Embarked_Q Is_Female
6 No Cabin 0 1 0


please help
use same grep command with where "." should be escape charachter
grep(pattern = "Moran, Mr\. James",x=tit$Age,fixed = T,value = T)
 
Hii i have tried to solve assignment 3 please help me last part where we require to find how many plane flew to destination once

#Questions for apply and dplyr family
head(iris)
?iris
str(iris)
#Find the sum of each column and confirm if the sum is greater than 800 or not
which(apply(iris[,1:4],MARGIN = 2,sum) > 800)
#Hint: create a custom function to find the sum and compare it with 800,
#and apply the function on each numerical column
fn = function(data)
{
sum(data)>800

}
apply(iris[,1:4],MARGIN = 2,fn)
#Find the sum / mean / median of Sepal Length species-wise
for (value in levels(iris$Species))
{
print(sum(iris$Sepal.Length[which(iris$Species==value)]))
print(mean(iris$Sepal.Length[which(iris$Species==value)]))
print(median(iris$Sepal.Length[which(iris$Species==value)]))
}
#For all the flowers having sepal width > 3.0, find the number of flowers in each species
table(iris$Species[which(iris$Sepal.Width>3.0)])
#Count how many different petal widths are there in each species.
for (value in levels(iris$Species))
{
print(length(table(iris$Petal.Width[which(iris$Species==value)])))
}
#Soln
?dplyr
library(dplyr)
iris%>%group_by(Species)%>%summarise(n_distinct(Petal.Width))
#Titanic Data set-------------------------------------------------------------
setwd("C:/Users/Alaska/Desktop/Simplilearn/Datascience with R/Live class/practicse")
train = read.csv("titanic_train.csv",na=c("","?","T"))
#Q. Find the proportion survived of males and females survived
aggregate(Survived ~ Sex, data=train, FUN=function(x) {round(sum(x)/length(x),digits=2)})
#Q. Find the proportion survived of males and females
aggregate(Survived ~ Pclass, data=train, FUN=function(x) {round(sum(x)/length(x),digits=2)})
#Q. Find the proportion of people survived passenger class wise and sex wise
aggregate(Survived ~ Pclass + Sex, data=train, FUN=function(x) {round(sum(x)/length(x),digits=2)})
#Q. Strip off the title from the names of the passengers, and create a sep col named title.
#Q. Ultimately we want only 4 levels in this col: Miss Mr. Mrs and Master
train$Title[train$Title %in% c('Lady', 'the Countess', 'Mlle')] <- 'Miss'
#if train$title is one among c('Lady', 'the Countess', 'Mlle') then give true / false
train$Title[train$Title %in% c('Capt', 'Don', 'Major', 'Sir', 'Col', 'Jonkheer', 'Rev', 'Dr') & train$Sex == "male" ] <- 'Mr'
train$Title[train$Title %in% c('Dona','Lady',"Dr","Mme", 'Ms') & train$Sex == "female"] <- 'Mrs'
?table
#hflights------------------------------------------------------------------------------------------
library(hflights)
install.packages("hflights")
#How many flights are not cancelled? Hint: use var cancellation code
table(hflights$CancellationCode)['C']
str(hflights)
hflights$CancellationCode
#Combine year month and day variables to create a date column
hflights$Date<- paste(hflights$DayofMonth,"/",hflights$DayofMonth,"/",
hflights$Year, sep = "")
hflights$Date
#Find the maximum AirTime for all flights whose Departure delay is not NA
max(hflights$AirTime[which(!is.na(hflights$DepDelay))], na.rm = T )
#Find per-carrier mean of arrival delays and arrange them in increasing / decreasing order
hflights%>%group_by(hflights$UniqueCarrier)%>%summarise(n_distinct(hflights$ArrDelay ))
table(hflights$UniqueCarrier, useNA="ifany")
hflights%>%group_by(UniqueCarrier)%>%mean(ArrTime , na.rm = T )
#How many airplanes only flew to one destination from Houston?
hflights[which(hflights$Origin == "HOU"),]->sam
which(table(sam$TailNum)==1)
# Hint: each tail number represents 1 airplane.
summarise_at(group_by(hflights,UniqueCarrier),vars(ArrTime),funs(mean(.,na.rm=TRUE)))
aggregate(hflights$ArrTime, by=list(hflights$UniqueCarrier), FUN=mean)
 
Hi Samridhi,

I think I do not have access to your google Drive to access the data sets and assignment files. I only have access to 11th Jan 2020 folder - (https://drive.google.com/drive/folders/1gq6lLJKuzH4u5PO1ftJ9JYGwFqaUik-U). Can I get the complete folder access to download the assignment files and the data sets for practicing.

Thanks in advance,
Raghavan K
Hi Samridhi, Not sure if you get to see my message. I am waiting for the response. Am I supposed to follow any different link for responses or something? I could not practice any of the assignments.
 
Top