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

Discussion in 'Big Data and Analytics' started by Shohini_1, Jan 10, 2020.

  1. Shohini_1

    Shohini_1 Well-Known Member
    Simplilearn Support

    Joined:
    Sep 24, 2018
    Messages:
    128
    Likes Received:
    17
    Hi All,

    Use this thread for your doubt clarification.
     
    #1
  2. Samridhi Dutta

    Samridhi Dutta Well-Known Member
    Alumni Trainer

    Joined:
    Aug 16, 2017
    Messages:
    193
    Likes Received:
    21
    #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"
     
    #2
  3. Abdul Rashid

    Abdul Rashid Member

    Joined:
    Dec 28, 2019
    Messages:
    4
    Likes Received:
    0
    Assignment
     

    Attached Files:

    #3
  4. Makarand Kulkarni

    Joined:
    Dec 30, 2019
    Messages:
    3
    Likes Received:
    0
    My assignment attached
     

    Attached Files:

    #4
  5. Kiruba Shankar

    Kiruba Shankar New Member

    Joined:
    Dec 1, 2019
    Messages:
    1
    Likes Received:
    0
    Here you go!!! :)

    for(i in 2:10)
    {
    for(j in 1:10)
    {
    cat( i , " X ", j , " = " , i*j , "\n", sep = "")
    }
    print(" \n")
    }
     
    #5
  6. Nithisha B R

    Nithisha B R New Member

    Joined:
    Jan 1, 2020
    Messages:
    1
    Likes Received:
    0
    for (i in 2:10) {
    for (j in 1:10) {
    cat(i,"X",j, "=", i*j,"\n",sep = "")
    }
    cat("\n\n")
    }
     
    #6
  7. Akshay_15

    Akshay_15 New Member

    Joined:
    Dec 16, 2015
    Messages:
    1
    Likes Received:
    0
    #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=" ")
    }
    }
     
    #7
  8. Kousik Das

    Kousik Das New Member

    Joined:
    Nov 19, 2019
    Messages:
    1
    Likes Received:
    0
    #========================================
    # 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))
    }
     
    #8
  9. Abhijit_78

    Abhijit_78 Member

    Joined:
    Nov 9, 2019
    Messages:
    2
    Likes Received:
    0
    for (a in c(2:10))
    {
    for (b in c(1:10))
    {
    print(paste(a, "*", b,"=", a * b))
    }
    }
     
    #9
  10. Shilpa_93

    Shilpa_93 Member

    Joined:
    Jan 4, 2020
    Messages:
    2
    Likes Received:
    0
    Hi , i need url to download R
     
    #10
  11. Sandip Gujar

    Sandip Gujar Member

    Joined:
    Dec 20, 2019
    Messages:
    5
    Likes Received:
    0
    Assignment 1
     

    Attached Files:

    #11
  12. Abhijit_78

    Abhijit_78 Member

    Joined:
    Nov 9, 2019
    Messages:
    2
    Likes Received:
    0
    #12
  13. Chinthalapally Manideep

    Joined:
    Jan 3, 2020
    Messages:
    2
    Likes Received:
    0
    pls go through it
     

    Attached Files:

    #13
  14. Aaditya Jittha

    Joined:
    Dec 27, 2019
    Messages:
    5
    Likes Received:
    0
    Hi Samridhi,

    PFA my assigment.

    Thanks,
    Aaditya
     

    Attached Files:

    #14
  15. Aaditya Jittha

    Joined:
    Dec 27, 2019
    Messages:
    5
    Likes Received:
    0
    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
     
    #15
  16. Debasish Biswas

    Joined:
    Dec 28, 2019
    Messages:
    3
    Likes Received:
    0
    for(i in 2:10){
    for(j in 1:3){
    print(paste(i,"X",j,"=",i*j))
    }
    }
     
    #16
  17. Prerna Sharma_3

    Joined:
    Dec 2, 2019
    Messages:
    2
    Likes Received:
    0
    my assignment file
     

    Attached Files:

    #17
  18. John_302

    John_302 Member

    Joined:
    Jan 2, 2020
    Messages:
    3
    Likes Received:
    0
     

    Attached Files:

    #18
  19. Samridhi Dutta

    Samridhi Dutta Well-Known Member
    Alumni Trainer

    Joined:
    Aug 16, 2017
    Messages:
    193
    Likes Received:
    21
    #19
  20. Nanar Chahverdian

    Joined:
    Jan 9, 2020
    Messages:
    2
    Likes Received:
    0
    for (num1 in 2:10) {
    cat('\n')
    for (num2 in 1:10) {
    cat(num1, 'x', num2, '=', num1*num2, '\n')
    }
    }
     
    #20
  21. Nanar Chahverdian

    Joined:
    Jan 9, 2020
    Messages:
    2
    Likes Received:
    0
    Dear Samridhi, can you please check below two questions?
    class(cat(1,2,3)) #why NULL?
    class(paste(1,2,3)) #why character?
     
    #21
  22. Gautam Lakshmiratan

    Gautam Lakshmiratan Active Member

    Joined:
    Dec 7, 2019
    Messages:
    17
    Likes Received:
    0
    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")
     
    #22
    Last edited: Jan 16, 2020
  23. Raghavan Komanduru

    Joined:
    Dec 24, 2019
    Messages:
    6
    Likes Received:
    0
    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?
     
    #23
    Last edited: Jan 17, 2020
  24. Arunkumar Rathinam

    Arunkumar Rathinam New Member

    Joined:
    Dec 17, 2019
    Messages:
    1
    Likes Received:
    0
    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")
    }
    }
     
    #24
  25. Gayathri Poovannan

    Gayathri Poovannan New Member

    Joined:
    Dec 17, 2019
    Messages:
    1
    Likes Received:
    0
    #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")
    }
    }
     
    #25
  26. Amit Sharma_49

    Joined:
    Dec 27, 2019
    Messages:
    2
    Likes Received:
    0
    outer = 10
    inner = 10

    for (i in 1:eek:uter )
    {
    for (j in 1:inner)
    {
    print(sprintf("%d X %d = %d", i , j , i * j))
    }
    cat("\n")
    }
     
    #26
  27. Amit Sharma_49

    Joined:
    Dec 27, 2019
    Messages:
    2
    Likes Received:
    0
    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"
    >
     
    #27
  28. John_302

    John_302 Member

    Joined:
    Jan 2, 2020
    Messages:
    3
    Likes Received:
    0
    Hi believe if you scroll down the console window, you should find the output immediately after the print of the code
     
    #28
  29. Gautam Lakshmiratan

    Gautam Lakshmiratan Active Member

    Joined:
    Dec 7, 2019
    Messages:
    17
    Likes Received:
    0
    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"
     

    Attached Files:

    #29
  30. Raghavan Komanduru

    Joined:
    Dec 24, 2019
    Messages:
    6
    Likes Received:
    0
    Issue is resolved by support team. Thanks
     
    #30
  31. Raghavan Komanduru

    Joined:
    Dec 24, 2019
    Messages:
    6
    Likes Received:
    0
    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
     
    #31
  32. nikhil suresh

    nikhil suresh Member

    Joined:
    Jan 3, 2020
    Messages:
    3
    Likes Received:
    0

    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
     
    #32
  33. Sujit Supekar

    Sujit Supekar New Member

    Joined:
    Dec 24, 2019
    Messages:
    1
    Likes Received:
    1
    Hi,
    does anyone have the chats saved from last week's sessions?
    Please share if yes.
    Thanks.
     
    #33
    Raghavan Komanduru likes this.
  34. Gautam Lakshmiratan

    Gautam Lakshmiratan Active Member

    Joined:
    Dec 7, 2019
    Messages:
    17
    Likes Received:
    0
    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?
     
    #34
  35. Raghavan Komanduru

    Joined:
    Dec 24, 2019
    Messages:
    6
    Likes Received:
    0
    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
     
    #35
  36. R Ramabadhran

    R Ramabadhran New Member

    Joined:
    Dec 13, 2019
    Messages:
    1
    Likes Received:
    0
    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
    }
    }


     
    #36
  37. Gautam Lakshmiratan

    Gautam Lakshmiratan Active Member

    Joined:
    Dec 7, 2019
    Messages:
    17
    Likes Received:
    0
    -----------------------
    could be this happening because of using "stringsAsFactors=TRUE" ? please help me to understand this!
     
    #37
  38. nikhil suresh

    nikhil suresh Member

    Joined:
    Jan 3, 2020
    Messages:
    3
    Likes Received:
    0
    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
     
    #38
  39. nikhil suresh

    nikhil suresh Member

    Joined:
    Jan 3, 2020
    Messages:
    3
    Likes Received:
    0
    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
     
    #39
  40. Abdul Rashid

    Abdul Rashid Member

    Joined:
    Dec 28, 2019
    Messages:
    4
    Likes Received:
    0
    use same grep command with where "." should be escape charachter
    grep(pattern = "Moran, Mr\. James",x=tit$Age,fixed = T,value = T)
     
    #40
  41. Abdul Rashid

    Abdul Rashid Member

    Joined:
    Dec 28, 2019
    Messages:
    4
    Likes Received:
    0
    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)
     
    #41
  42. Raghavan Komanduru

    Joined:
    Dec 24, 2019
    Messages:
    6
    Likes Received:
    0
    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.
     
    #42
  43. Raghavan Komanduru

    Joined:
    Dec 24, 2019
    Messages:
    6
    Likes Received:
    0
    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..
     
    #43
  44. John_302

    John_302 Member

    Joined:
    Jan 2, 2020
    Messages:
    3
    Likes Received:
    0
    My Assignment
     

    Attached Files:

    #44
  45. Akanksha Chaudhary

    Akanksha Chaudhary Active Member
    Alumni

    Joined:
    Feb 20, 2019
    Messages:
    20
    Likes Received:
    0
    Hi
    Hi Shohini , please can u make a thread for Tableau Discussions & Python too
     
    #45
  46. Prerna Sharma_3

    Joined:
    Dec 2, 2019
    Messages:
    2
    Likes Received:
    0
    my assignment
     

    Attached Files:

    #46
  47. Makarand Kulkarni

    Joined:
    Dec 30, 2019
    Messages:
    3
    Likes Received:
    0
    My assignement -1
     

    Attached Files:

    #47
  48. Gautam Lakshmiratan

    Gautam Lakshmiratan Active Member

    Joined:
    Dec 7, 2019
    Messages:
    17
    Likes Received:
    0
    Error in Titanic$survived : $ operator is invalid for atomic vectors this is get when i try to access any columns of the database.
     
    #48
  49. Samridhi Dutta

    Samridhi Dutta Well-Known Member
    Alumni Trainer

    Joined:
    Aug 16, 2017
    Messages:
    193
    Likes Received:
    21
    #49
  50. Gautam Lakshmiratan

    Gautam Lakshmiratan Active Member

    Joined:
    Dec 7, 2019
    Messages:
    17
    Likes Received:
    0
    "how can i change it back to character vectors?"
     
    #50

Share This Page