### Welcome to the Simplilearn Community

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

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

#### Shohini_1

Staff member
Simplilearn Support
Alumni
Customer
Hi All,

#### 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"

Assignment

#### Attachments

• Assignment.txt
1.7 KB · Views: 25

#### Makarand Kulkarni

##### Member
My assignment attached

#### Attachments

• Makarand_assignment_1.txt
109 bytes · Views: 13

#### Kiruba Shankar

##### Member
Here you go!!!

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

#### Nithisha B R

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

#### 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))
}

#### Abhijit_78

##### Member
for (a in c(2:10))
{
for (b in c(1:10))
{
print(paste(a, "*", b,"=", a * b))
}
}

Assignment 1

#### Attachments

• Assignment 1 from Sandip Gujar.txt
1.9 KB · Views: 7

#### Chinthalapally Manideep

##### Member
pls go through it

#### Attachments

• assignment 1.txt
91 bytes · Views: 4

##### Member
Hi Samridhi,

PFA my assigment.

Thanks,

#### Attachments

• Assignment.txt
67 bytes · Views: 3

##### Member
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,

#### Debasish Biswas

##### Member
for(i in 2:10){
for(j in 1:3){
print(paste(i,"X",j,"=",i*j))
}
}

#### Prerna Sharma_3

##### Member
my assignment file

#### Attachments

• tables.txt
124 bytes · Views: 1

#### John_302

##### 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"

#### Attachments

• Assignment 1.txt
102 bytes · Views: 2

#### Nanar Chahverdian

##### 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"

for (num1 in 2:10) {
cat('\n')
for (num2 in 1:10) {
cat(num1, 'x', num2, '=', num1*num2, '\n')
}
}

#### Nanar Chahverdian

##### Member
Dear Samridhi, can you please check below two questions?
class(cat(1,2,3)) #why NULL?
class(paste(1,2,3)) #why character?

#### 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

Last edited:

#### Raghavan Komanduru

##### 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?

Last edited:

#### Arunkumar Rathinam

##### 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"

Hey Samridhi!

Here's the Assignment - "Multiplication Tables"

i = NULL
j = NULL
for( i in 2:10)
{
for(j in 1:10)
{
k = i*j
cat(i,"x",j,"=",k,"\n")
}
}

#### Gayathri Poovannan

##### New Member
#Assignment for tables
i = NULL
j = NULL
for( i in 2:10)
{
for(j in 1:10)
{
k = i*j
cat(i,"x",j,"=",k,"\n")
}
}

#### Amit Sharma_49

##### Member
outer = 10
inner = 10

for (i in 1uter )
{
for (j in 1:inner)
{
print(sprintf("%d X %d = %d", i , j , i * j))
}
cat("\n")
}

#### Amit Sharma_49

##### Member
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"

[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

#### Raghavan Komanduru

##### 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?
Issue is resolved by support team. Thanks

#### Raghavan Komanduru

##### Member
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

#### nikhil suresh

##### 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"

c <- 2
for (i in 1:10){
print(paste(c,"x",i,"=",c*i))
}

This is for 2 table ...same thing can be changed to 3 ,4 ,5 and so on

#### Sujit Supekar

##### Member
Hi,
does anyone have the chats saved from last week's sessions?
Thanks.

#### Gautam Lakshmiratan

##### Active Member
receiving an error is R Studio
err msg:" the process cannot access the file because it is being used by another process"

how do we resolve this?

#### Raghavan Komanduru

Hi Samridhi,

Raghavan K

##### Member
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
-----------------------

#### nikhil suresh

##### Member
Hi Samridhi,

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

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

output: character(0)

#### nikhil suresh

##### Member
Hi Samridhi,

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

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

#### Abdul Rashid

##### Member
Hi Samridhi,

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

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

use same grep command with where "." should be escape charachter
grep(pattern = "Moran, Mr\. James",x=tit\$Age,fixed = T,value = T)

#### Abdul Rashid

##### Member
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
?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-------------------------------------------------------------
#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)

#### Raghavan Komanduru

##### Member
Hi Samridhi,

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.

#### Raghavan Komanduru

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

John,
There was an issue with the key word Ctrl+Enter. I have raised a support ticket and they addressed it. It is now working fine. Thanks for the concern..

My Assignment

#### Attachments

• 1. Assignment.zip
12.3 KB · Views: 6

#### Akanksha Chaudhary

##### Active Member
Alumni
Hi
Hi All,

Hi Shohini , please can u make a thread for Tableau Discussions & Python too

my assignment

#### Attachments

• assignment.txt
2 KB · Views: 4

#### Makarand Kulkarni

##### Member
My assignement -1

#### Attachments

• Makarand.1.Assignment.txt
1.4 KB · Views: 3

#### Gautam Lakshmiratan

##### Active Member
Error in Titanic\$survived : \$ operator is invalid for atomic vectors this is get when i try to access any columns of the database.

#### Gautam Lakshmiratan

##### Active Member
Error in Titanic\$survived : \$ operator is invalid for atomic vectors this is get when i try to access any columns of the database.

"how can i change it back to character vectors?"