# DS with R | Dec 22 - Jan 27 | Samridhi

Discussion in 'Big Data and Analytics' started by Nishant_Singh, Dec 21, 2018.

Joined:
Aug 1, 2018
Messages:
312
109
#1
2. ### _48134 Member

Joined:
Nov 13, 2018
Messages:
2
0
hi team,

this is Chandu here..

#2
3. ### _48134 Member

Joined:
Nov 13, 2018
Messages:
2
0
#Assignment -1 : to print 2 table
# Solution
i= 1
while(i < 11)
{
paste("2 * ", i," = ",2*i) -> j
print (j)
i = i+1
}

#3
4. ### _44674 New Member

Joined:
Oct 22, 2018
Messages:
1
0
# Assignment 1 - To print multiplication table of 2
a=c(1:10)
b=NULL
for (i in a)
{
paste("2 X",i,"=",2*a)->b
print(b)
}

# Assignment 2 - to print the following
#1111
# 111
# 11
# 1
lines=c(1:4)
cols=c(1:4)
a=NULL
for (i in lines)
{
for(j in cols)
{
if (j<i)
next
1->a[j]
b<-as.integer(a)
b[which(is.na(b))]=0
}
print(b)
a=NULL
}

#4
5. ### _52654 New Member

Joined:
Dec 19, 2018
Messages:
1
0
#Assignment 1: Print table of 2
Val1=c(1:10)
tab1=NULL
for (i in val1)
{
paste("2 X",val1,"=",2*val1)->tab1
}
print(tab1)

#5
Last edited: Jan 4, 2019
6. ### _46256 Member

Joined:
Oct 31, 2018
Messages:
5
0
# Assignment1 - print the table of 2
# 2 x 1 = 2 ....

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

#6
7. ### _51728 Member

Joined:
Dec 12, 2018
Messages:
5
0
#nested loop to print inverted right angle triangle of 1
rows=1:4
cols=1:4
p=1
for (i in rows)
{
for (j in cols)
{
ifelse((j<=4 & j>=i), yes = "1", no ="" )->p[j]
p[j]=as.numeric(p[j])
j=j+1
}
ifelse((i<=4 & i>j), yes = break , no = "1" )->q
print(p)
i=i+1
}

#7
8. ### _51728 Member

Joined:
Dec 12, 2018
Messages:
5
0
#multiplication of 2
a = 1:10
#b = 2
for(i in a)
{
b = as.integer(b)
if(i<=10)
x=b*a
paste(b,"X",a,"=",x)->y[x]
print(y[x])
i=i+1
break
}

#8
9. ### Manish Rawat Member

Joined:
Jun 13, 2016
Messages:
3
0
Class2:
Assignment 1 (Multiplication table of 2)
a=2
for (i in c(1:10))
{
cat(a," * ",i,"=",a*i,"\n")
}

Assignment 2
rows= 1:4
cols = 1:4
for(i in rows)
{
for(j in cols)
{
if(i>j)
{
a=paste(" ",sep="")
}else
{
a=paste(1,sep="")
}
cat(a)
}
cat("\n")
}

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Class3:
Assignment 3, from mt matrix delete col named wt and vs

mt<-mtcars
colnames(mt)
which(colnames(mt)=='wt')
which(colnames(mt)=='vs')
mt[,-c(6,8)]->a
colnames(a)

Assignment 4
interchange the position of column classB and classD in mat

matrix(data=c(1,10,20,4,6,8),nrow=2, ncol=3)->mat
colnames(mat)=c("ClassA","ClassB", "ClassC")
rownames(mat)=c("schoolA","schoolB")
schoolc=c(20, 10, 45)
rbind(mat, schoolc)->mat
ClassD=c(10,4,12)
cbind(mat,ClassD)->mat
mat[,c("ClassA","ClassD","ClassC", "ClassB")]

Manish Rawat

#9
10. ### John Paul Dysangco Member

Joined:
Dec 11, 2018
Messages:
3
0
Hi Samridhi,

Can you please save the class 4 notes from yesterday to the shared Google Drive?

Thanks,
JP

#10
11. ### _51728 Member

Joined:
Dec 12, 2018
Messages:
5
0
class 4 notes and assignments not shared yet?

#11

Joined:
Aug 16, 2017
Messages:
250
22
Done!

#12
13. ### _46256 Member

Joined:
Oct 31, 2018
Messages:
5
0
# Assignment3: From this mt matrix, delete wt and vs columns
mtcars -> mt
mt
# to get index of wt column name
grep("^wt", colnames(mt))->x
x
mt[, -x] -> mt
View(mt)

# to get index of wt column name
grep("^vs", colnames(mt))->x
x
mt[, -x] -> mt
View(mt)

#Assignment4: Interchange the position of classB and classD
mat[,c(2,4)] -> mat[,c(4,2)]
mat

colnames(mat)[c(2,4)] -> colnames(mat)[c(4,2)]
mat

#13
14. ### _52033 Member

Joined:
Dec 14, 2018
Messages:
2
0
How to retrieve iris data set. Got accidentally erased

#14
15. ### _46256 Member

Joined:
Oct 31, 2018
Messages:
5
0
# Submission by Ashish R
# Assignment on DF
#Use the data-set iris
View(iris) #iris is a pre-loaded data-set in r
iris\$Sepal.Length
#Q. Find the average sepal length of Setosa species. Hint: use function "mean"
#Hint: Following is the mean function
mean(iris\$Sepal.Length, na.rm = T)
#Note: na.rm = T will remove nas while finding mean

#Q. Find the different categories of species variable. Hint use "table" function on Species variable
iris\$Species
table(iris\$Species)
levels(iris\$Species)

#Q. Find which species has the highest Petal Width.
iris\$Species[iris\$Petal.Width == max(iris\$Petal.Width)]

#Q. For all the virginica flowers having petal width = 2.0, make their petal length to 5.5
iris\$Petal.Length[iris\$Petal.Width == 2.0] = 5.5
iris\$Petal.Length[iris\$Petal.Width == 2.0]

#titanic dataset

#Q. Find the number of passengers who survived
sum(titanic_train\$Survived)

#Q. Find the age of Timothy
nm_Age = grep("Timothy", titanic_train\$Name, ignore.case= TRUE)
nm_Age
titanic_train\$Age[nm_Age]

View(titanic_train)

#Q. Find names of passengers having age>35
titanic_train\$Name[titanic_train\$Age>35]

#Q. The age of Henry has been entered incorrect. Please change it to 45.
nm_Age = grep("Henry", titanic_train\$Name, ignore.case= TRUE)
nm_Age
titanic_train\$Age[nm_Age]
titanic_train\$Age[nm_Age] = 45
titanic_train\$Age[nm_Age]

#Q. How many NAs are there in Age column. Replace the NAs by the mean age.Following fn will give the mean age:
# find out NAs inAge column
sum(is.na(titanic_train\$Age))
m_age = mean(titanic_train\$Age,na.rm = TRUE)
m_age

#Q. Add another column: Family_Members = 1(Self) + SibSp (gives number of siblings + spouse) + Parch (gives parents + children)
class(titanic_train)
?data.frame
?cbind.data.frame
names(Family_members)
titanic_train\$Name

cbind(titanic_train, Family_members = factor(1+titanic_train\$SibSp+titanic_train\$Parch)) -> titanic_train

View(titanic_train)

#15

Joined:
Nov 22, 2018
Messages:
3
0
Hi Guys,

#16
17. ### _52033 Member

Joined:
Dec 14, 2018
Messages:
2
0
In the lms.simplilearn.com, > the self learning tab >Course Resources> Projects. Here you can download the projects

#17
18. ### _52616 Member

Joined:
Dec 18, 2018
Messages:
12
1
Hello, I'm trying to watch the classes 3 and 4 from "Downloading Records" but they are empty.

#18
19. ### _52616 Member

Joined:
Dec 18, 2018
Messages:
12
1
#submission by Mario Garcia (_52616)
#Assignment on data-frames

#Use the data-set iris
View(iris) #iris is a pre-loaded data-set in r
iris\$Sepal.Length
#Q. Find the average sepal length of Setosa species. Hint: use function "mean"
#Hint: Following is the mean function
mean(iris\$Sepal.Length, na.rm = T)#na.rm = T will remove nas while finding mean
#rta: 5.8433
#Q. Find the different categories of species variable. Hint use "table" function on Species variable
table(iris\$Species)
levels(iris\$Species)
?table
?levels
# levels provides access to the levels attribute of a variable. The first form returns the value of the levels of its argument and the second sets the attribute.
# table uses the cross-classifying factors to build a contingency table of the counts at each combination of factor levels.

table(iris\$Sepal.Length)
table(iris\$Sepal.Width)
hist(iris\$Sepal.Width)

#Q. Find which species has the highest Petal Width.
max_petal_with <- max(iris\$Petal.Width)
iris\$Species[iris\$Petal.Width == max_petal_with]
#response: max_petal_with = 2.5
#response: species "Virginica"

#Q. For all the virginica flowers having petal width = 2.0, make their petal length to 5.5
petal_width = 2.0
iris_species_viginica <- iris[ iris\$Species == 'virginica', ]
iris_species_viginica_petal_width_20 <- iris_species_viginica[ iris\$Sepal.Width == 2.0, ]
iris_species_viginica_petal_width_20\$Petal.Length = 5.5

#titanic dataset
getwd()
View(titanic_train)
str(titanic_train)

#Q. Find the number of passengers who survived
survived <- 1
titanic_train_survived = titanic_train[ titanic_train\$Survived == survived ,]
count(titanic_train_survived)
#response: 342
#Q. Find the age of Timothy
name = 'Timothy'
titanic_train_by_name_idx <- grep(name, titanic_train\$Name, ignore.case=TRUE)
titanic_train_by_name_idx
titanic_train[ titanic_train_by_name_idx, c('PassengerId', 'Name', 'Age') ]
#response: McCarthy, Mr. Timothy J is 54 years old

#Q. Find names of passengers having age>35
age <- 35
titanic_train_by_age <- titanic_train[ titanic_train\$Age > age, ]
View(titanic_train_by_age)
titanic_train_by_age\$Name
#response:
#Q. The age of Henry has been entered incorrect. Please change it to 45.
name = 'Henry,'
age = 45
titanic_train_by_name_idx = grep(name, titanic_train\$Name, ignore.case=TRUE)
titanic_train_by_name_idx
titanic_train_by_name <- titanic_train[ titanic_train_by_name_idx, ]
titanic_train_by_name
titanic_train_by_name\$Age <- age
#Q. How many NAs are there in Age column. Replace the NAs by the mean age.Following fn will give the mean age:
titanic_train_by_age_idx = is.na( titanic_train\$Age )
titanic_train_by_age_idx
titanic_train_by_age = titanic_train[ titanic_train_by_age_idx, ]
count(titanic_train_by_age)
#response: there are 177

age_mean <- mean(titanic_train\$Age,na.rm = TRUE)
age_mean
titanic_train_by_age\$Age <- age_mean
titanic_train_by_age

#Q. Add another column: Family_Members = 1(Self) +
# SibSp (gives number of siblings + spouse) +
# Parch (gives parents + children)

?cbind
# Take a sequence of vector, matrix or data-frame arguments and combine by columns or rows, respectively. These are generic functions with methods for other R classes.
titanic_train <- cbind( titanic_train, Family_Members = factor( 1 + titanic_train\$SibSp + titanic_train\$Parch ))
View(titanic_train )

#19
20. ### _52616 Member

Joined:
Dec 18, 2018
Messages:
12
1
When I answered the "knowledge check", some characters are "?" symbol. How can I get the right character? I think that is a problem of char encoder

#20
21. ### _55153 Member

Joined:
Jan 8, 2019
Messages:
2
0
Hi Samridhi ,
Could you please my help me on the hypothesis questions which i have on one of the exercise which i found online

I am trying to solve an hypothesis question which i believe it is similar to students score example related to Ramesh tutor example you explained in the class. I believe i understood the example and i thought i got it . But when iam soliving another similar example i have variety of doubts. Hoping to get it resolved . Iam attaching the doc which is having background and questions are highlighted in red color. Iam unable to attach the dataset because of restriction of size and types of files.

File size:
15.5 KB
Views:
4
#21
22. ### Samridhi Dutta Well-Known Member AlumniTrainer

Joined:
Aug 16, 2017
Messages:
250
22
Hi, I will need the dataset and the .R file to help you with this. Without comprehending the data, it is difficult to comprehend the problem statement. Can you please share the data-set in a zip file. If it is too large for an upload here, could you please upload on google drive and share the link here.

Regards,
Samridhi

#22
Last edited: Mar 13, 2019

Joined:
Jan 8, 2019
Messages:
2
0
#23

Joined:
Aug 16, 2017
Messages:
250
22
#24
25. ### Samridhi Dutta Well-Known Member AlumniTrainer

Joined:
Aug 16, 2017
Messages:
250
22
I am attaching my solution here. I feel there is a flaw in the logic of the code shared. We have to compare the adherence marks of those who certified before / after 1990. We don't have the certification scores in our data. So, we don't have a relation with that. We will not have to create multiple dataframes to divide the adherence scores based on before / after 1990.

I have shared my perspective to the problem in the attached file. Let me know if there are any questions which need more explanation.

Regards,
Samridhi

#### Attached Files:

• ###### solution_code.txt
File size:
5.1 KB
Views:
2
#25

Joined:
Feb 25, 2019
Messages:
4
0
#Assignment 2 - Vectors
a = c(1:4)
b = c(10,20,30, 40)
c = c(100:110)
#Q. Perform the following operations on the above vectors
a + b
a * b
a / b
a %% b
b+c -> d
b*c -> e
c-b -> f
c/b
c %/%b
#Q. Please state your observation on what happens when arithmetic operators are applied on c and b
#ans: b gets applied repeatedly
#Q. How many components are there in the vector c
length(c)
#ans: : 11
#Q. Delete the last 5 components of the vector c
c <- c[-(length(c)-4:length(c))]
#Q. Strip off the beginning space char from the following character vector
strings = c(" Astha Ahuja", " Beena Ahuja", " Charu Raheja")
strings =sub(".? ","",strings)
#Q. Find all the components of the vector that begin with new (ignore the case)
Strings = c("New York", "New Mumbai", "Mumbai new")
#Soln: grep("^new", strings, ignore.case = T)
strings2 <-grep(pattern = "^New", x=Strings)
Strings[strings2]
#Q. Find all the components of the vector that end with the new (ignore the case)
Strings = c("New York", "New Mumbai", "Mumbai new")
#Soln: grep("new\$", strings, ignore.case = T)
strings2 <-grep(pattern = "new\$", x=Strings)
Strings[strings2]
#Q. Replace " ." with space character
Strings = c("Divya.Ahuja", "Mukul.Khanna")
Strings = gsub("\\.","\\s ",Strings)
Strings

#26

Joined:
Aug 16, 2017
Messages:
250