Welcome to the Simplilearn Community

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

Sign Up

DATA SCIENCE WITH R | JAN 16 | Sabyasanchi

If anyone needs any help on today's assignment let everyone know. Assignments are

## Assignment 1 :

# v <- c( 23,45,66,38,50,44,67,83)
# Add 5 to the elements of the vector if its odd number , else add 10 to the element

## Assignment 2 :
# v <- ("Ravi","Sunil","Anjali","Tejas","Victor","Suraj","Abhishek")
# Print the names which begin with "A"
# Print the names in lower case .
# Print the names sorted in alphapetical order .
 

Sunil Kumar_62

Active Member
Earlier SABYASANCHI Sir said Feb. 6-7 are important sessions? Can we get to know what are the topics for those days? Also Can we get to know about the placement assistance?
 

Sunil Kumar_62

Active Member
new_function = function(a,b,c){
result=(a+b)*c
return(result)
}
new_function = (2,3,4)

Error:
> new_function = function(a,b,c){
+ result=(a+b)*c+ return(result)+ }> > new_function = (2,3,4)Error: unexpected ',' in "new_function = (2,"
>
@ Venkatesan IN THIS YOUR GIVEN ASSIGNMENT OPERATOR IS '=' INSTEAD COMMONLY USED ONE IS <-. Please try changing your assignment operator and see if still error persists.
 

Sinu Jose

Active Member
Hello All,

I tried assignment #1. Hope it helps :)

################################################################

# Assignment 1
# v <- c(23,45,66,38,50,44,67,83)
# Add 5 to the elements of vector if odd, else 10 if even

v <- c(23,45,66,38,50,44,67,83)
print(v)

for(i in v) {
if(i%%2 != 0) {
j=i+5
print(paste(i,"is odd and the new value is ",j))
}else{
j=i+10
print(paste(i,"is even and the new value is ",j))
}
}

######################################################
 

Sinu Jose

Active Member
new_function = function(a,b,c){
result=(a+b)*c
return(result)
}
new_function = (2,3,4)

Error:
> new_function = function(a,b,c){
+ result=(a+b)*c+ return(result)+ }> > new_function = (2,3,4)Error: unexpected ',' in "new_function = (2,"
>

While calling the function, we need to call as below (assignment operator was there while calling which is not right as per syntax)
new_function(2,3,4)
 

Sinu Jose

Active Member
Tried Assignment # 2. Hope this is what expected :)

########################################################
# Assignment 2
# Given below the vector
v_vec1 <- c('Ravi','Anjali','Sunil','Tejas','Victor','Suraj','Abhijit')
print(v_vec1)


# 1. Print the names begin with 'A'

for (i in v_vec1) {
# Print the name begin with A
if (toupper(substr(i,1,1))=='A') {
print(paste('Names starting with A:',i))
}
}

# 2. Print the names in lowercase

for (i in v_vec1) {
# Convert each name to lowercase
print(paste('original -',i,' After lowercase conversion - ',tolower(i)))
}

# 3. Sort the names in vector

sort(v_vec1)

#################################################
 

Sunil Kumar_62

Active Member
In the given assignment, I have added 5 to odd and 10 to even numbers in vector as said but I used Indexing to do this. Existing numbers were updated with 5 and 10 respectively!
 
new_function= function(a,b,c){
output= (a+b)*c
return(output)
}
new_function(1,2,4)
new_function(3,2,3)
new_function(5,5,0)
new_function(2,1,3)
new_function(1,4,3)
new_function(1,2,7)



i think there should not be any "=' symbol while giving new function
 
new_function = function(a,b,c){
result=(a+b)*c
return(result)
}
new_function = (2,3,4)

Error:
> new_function = function(a,b,c){
+ result=(a+b)*c+ return(result)+ }> > new_function = (2,3,4)Error: unexpected ',' in "new_function = (2,"
>

#### this worked as well
new_function = function(a,b,c)
{ result=((a+b)*c)
return (result)
}
new_function(1,2,3)
new_function(4,5,6)
 
I was able to convert a given string into upper as well as lower case. as given in the assignment...was able to do other tasks as well but I want to ask if there is a way to get a string in a specific format, in this case mixed case. What I mean is that "simplilearn" should be printed as "SiMpLiLeArN". Kindly advise

### assignment functions
chart= c("Ravi","Sunil","Anjali","Tejas","Victor","Suraj","Abhishek")
chart [grep("A", chart)] #print the names that begin with "A"
sort(chart,decreasing = FALSE) #print names in alphabetical order
for (name in chart) #print names in lower case
{print(tolower(name))}
for (name in chart) #print names in upper case
{print(toupper(name))}
 
If anyone needs any help on today's assignment let everyone know. Assignments are

## Assignment 1 :

# v <- c( 23,45,66,38,50,44,67,83)
# Add 5 to the elements of the vector if its odd number , else add 10 to the element

## Assignment 2 :
# v <- ("Ravi","Sunil","Anjali","Tejas","Victor","Suraj","Abhishek")
# Print the names which begin with "A"
# Print the names in lower case .
# Print the names sorted in alphapetical order .

Could you please assist in understanding?
 
Hello All,

I tried assignment #1. Hope it helps :)

################################################################

# Assignment 1
# v <- c(23,45,66,38,50,44,67,83)
# Add 5 to the elements of vector if odd, else 10 if even

v <- c(23,45,66,38,50,44,67,83)
print(v)

for(i in v) {
if(i%%2 != 0) {
j=i+5
print(paste(i,"is odd and the new value is ",j))
}else{
j=i+10
print(paste(i,"is even and the new value is ",j))
}
}

######################################################


can you just tell me why we are using ! = here?
 

Himank mathur

New Member
If anyone needs any help on today's assignment let everyone know. Assignments are

## Assignment 1 :

# v <- c( 23,45,66,38,50,44,67,83)
# Add 5 to the elements of the vector if its odd number , else add 10 to the element

## Assignment 2 :
# v <- ("Ravi","Sunil","Anjali","Tejas","Victor","Suraj","Abhishek")
# Print the names which begin with "A"
# Print the names in lower case .
# Print the names sorted in alphapetical order .

lets do for assignment 2
let me know the codes for the same pls
 
Tried Assignment # 2. Hope this is what expected :)

########################################################
# Assignment 2
# Given below the vector
v_vec1 <- c('Ravi','Anjali','Sunil','Tejas','Victor','Suraj','Abhijit')
print(v_vec1)


# 1. Print the names begin with 'A'

for (i in v_vec1) {
# Print the name begin with A
if (toupper(substr(i,1,1))=='A') {
print(paste('Names starting with A:',i))
}
}

# 2. Print the names in lowercase

for (i in v_vec1) {
# Convert each name to lowercase
print(paste('original -',i,' After lowercase conversion - ',tolower(i)))
}

# 3. Sort the names in vector

sort(v_vec1)

#################################################
Assignment #2
We could try this way; which is easier to achieve o/p in 1 line


name1 <- c("Ravi","Sunil","Anjali","tejas","VICTOR","Suraj","Abhishek")
# Print the names which begin with "A"
# Print the names in lower case .
# Print the names sorted in alphapetical order .

name1[grep("A",name1)]
sort(name1, decreasing = FALSE)
name1[grepl("^[[:lower:]]+$", name1)]
 

rujuta_1

Member
@PRRao here is the code
num<-20

if(num %% 2 ==0){
print("number is even")
} else if(num %% 2 ==1)
{
print("number is odd")
}else
{
print("number is zero")
}
 
Last edited:
####Assignment - Jan 17th

#1.
#odd +5
#even +10
v1 <- c(23,45,66,38,50,44,67,83)

for(value in v1){
newvector <- ifelse(value %% 2 == 0, value + 10,value +5)
print(paste(value," changed to ",newvector))
}


#2.
v2<- c("ravi","Sunil","Anjali","Tejas","Victory","Suraj","Abishek")

#print names begins with A
print(sort(v2,decreasing = FALSE))

# print names in lower case
for(val in v2){
print(ifelse(grepl("^[[:lower:]]", val) == TRUE, val, ''))
}

#print names sorted in alphabetical order
for(val in v2){
print(ifelse(grepl('^A', val) == TRUE, val,''))
}
 

Attachments

  • R1.jpg
    R1.jpg
    163.2 KB · Views: 16
## Assignment 1 17/01/2021:

# v <- c( 23,45,66,38,50,44,67,83)
# Add 5 to the elements of the vector if its odd number , else add 10 to the element

v <- c(23,45,66,38,50,44,67,83)
print(v)
for(i in v) {
if(i%%2 != 0) {
j=i+5
print(j)
}else{
j=i+10
print(j)
}
}
 
#Solution to Assignment given by Sir on 17 jan 2021

var <- c("Ravi","Sunil","Anjali","Tejas","Victor","Suraj","Abhishek")

# Print the names which begin with "A"
grep("A",var,value = TRUE)

# Print the names in lower case .
tolower(var)

# Print the names sorted in alphapetical order
sort(var)​
 
## Assignment2 17/01/2021 :

v <- c( 23,45,66,38,50,44,67,83)

# Add 5 to the elements of the vector if its odd number , else add 10 to the element

for(i in v){
if (i%%2==0){
print(paste('i is even adding 5 to number',i+5))
}else{
print(paste('i is odd adding 10 to number',i+10))
}
}​
 
# Assignment function which return the sum two vector 23/01/21

ve1 = c(1,2,3,4,5,6,7)
ve2= c(2,3,2,3,2,3,2)

my_sum = function(v1,v2){
res= v1+v2
return(res)
}

print(my_sum(ve1,ve2))
 
Hello All,

I tried assignment #1. Hope it helps :)

############################################## ##############

# Assignment 1
# v <- c(23,45,66,38,50,44,67,83)
# Add 5 to the elements of vector if odd, else 10 if even

v <- c(23,45,66,38,50,44,67,83)
print (v)

for(i in v) {
if(i%%2 != 0) {
j=i+5
print(paste(i,"is odd and the new value is ",j))
}else{
j=i+10
print(paste(i,"is even and the new value is ",j))
}
}

######################################################
is there other way can we solve it , if yes please share it as well .

thanks
 
my first code and a solution for 1st assignment :)

v <- c(23,45,66,38,50,44,67,83)
print (v)
for(i in v) {
if(i%%2 == 0)
{j=i+10
print(paste (i, "is even","new value is",j))}
else {
j=i+5
print(paste(i,"is odd","new value is",j ))}
}
 

Sabyasachi Tripathy

Customer
Customer
## Assignment 1 17/01/2021:

# v <- c( 23,45,66,38,50,44,67,83)
# Add 5 to the elements of the vector if its odd number , else add 10 to the element

v <- c(23,45,66,38,50,44,67,83)
print(v)
for(i in v) {
if(i%%2 != 0) {
j=i+5
print(j)
}else{
j=i+10
print(j)
}
}


Looks good .
Can you also try a for loop something like this ?

for ( i in 1:length(vec) ) {
}
 

Sabyasachi Tripathy

Customer
Customer
####Assignment - Jan 17th

#1.
#odd +5
#even +10
v1 <- c(23,45,66,38,50,44,67,83)

for(value in v1){
newvector <- ifelse(value %% 2 == 0, value + 10,value +5)
print(paste(value," changed to ",newvector))
}


#2.
v2<- c("ravi","Sunil","Anjali","Tejas","Victory","Suraj","Abishek")

#print names begins with A
print(sort(v2,decreasing = FALSE))

# print names in lower case
for(val in v2){
print(ifelse(grepl("^[[:lower:]]", val) == TRUE, val, ''))
}

#print names sorted in alphabetical order
for(val in v2){
print(ifelse(grepl('^A', val) == TRUE, val,''))
}
Great !!!

for #1 : try to provide the output in another vector. This is a good way of showing the results .

for the others you may try substring as well and sort function we learnt yesterday
 
@Sabyasachi Tripathy
sir i am using demo1 data set in csv for doing data frame operation here so after grouping data using col job and marital and sum of durations and then ordering it using job and marital then result i am getting it mostly NA in those columns can u please help on this


my code :
group1 = aggregate(x = dfc$duration , by = list(dfc$job) , FUN = mean)
group1
group = aggregate(x = dfc$duration , by = list(dfc$job,dfc$marital) , FUN = sum)
group

names(group) = c("job","marital","sum")
head(group)
group[order(dfc$job,dfc$marital),]


result :
group[order(dfc$job,dfc$marital),]
job marital sum

NA <NA> <NA> NA
NA.1 <NA> <NA> NA
NA.2 <NA> <NA> NA
NA.3 <NA> <NA> NA
NA.4 <NA> <NA> NA
NA.5 <NA> <NA> NA
NA.6 <NA> <NA> NA
NA.7 <NA> <NA> NA
NA.8 <NA> <NA> NA
NA.9 <NA> <NA> NA
NA.10 <NA> <NA> NA
NA.11 <NA> <NA> NA
NA.12 <NA> <NA> NA
NA.13 <NA> <NA> NA
NA.14 <NA> <NA> NA
NA.15 <NA> <NA> NA
NA.16 <NA> <NA> NA
NA.17 <NA> <NA> NA
NA.18 <NA> <NA> NA
NA.19 <NA> <NA> NA
NA.20 <NA> <NA> NA
NA.21 <NA> <NA> NA
NA.22 <NA> <NA> NA
NA.23 <NA> <NA> NA
NA.24 <NA> <NA> NA
NA.25 <NA> <NA> NA
NA.26 <NA> <NA> NA
NA.27 <NA> <NA> NA
NA.28 <NA> <NA> NA
NA.29 <NA> <NA> NA
NA.30 <NA> <NA> NA
NA.31 <NA> <NA> NA
NA.32 <NA> <NA> NA
NA.33 <NA> <NA> NA
A> NA
NA.67 <NA> <NA> NA
NA.68 <NA> <NA> NA
12 admin. married 58628
30 self-employed single 15400
NA.69 <NA> <NA> NA
NA.70 <NA> <NA> NA
NA.71 <NA> <NA> NA
NA.72 <NA> <NA> NA
NA.73 <NA> <NA> NA

[ reached 'max' / getOption("max.print") -- omitted 4188 rows ]


>




Please help me on this
 

Sunil Kumar_62

Active Member
Thanks to Sabyasanchi sir, I am now able to do data analysis of my own data choice. I have chosen the Glucose intolerance capstone project excel document and converted it successfully into a CSV file then did the analysis and checked for errors or null values.



I did break everything, I used all the code on it from rbind lbind, to index, ncol, row list, is.na, checked many things randomly and saw the behavior of data, and analyzed the results successfully.



Was able to work on some of the given assignments confidently.



Special Thanks to Sabyasanchi

Best Regards,
Sunil Kumar
 

Attachments

  • Screenshot (54).png
    Screenshot (54).png
    185.9 KB · Views: 14
  • Screenshot (56).png
    Screenshot (56).png
    202.9 KB · Views: 15
Last edited:
#### Assignment : print the count of NAs in individual column.
# run a for loop on names(dataframe)


for (sunny in (names(US_Car_df))){
if((is.na(names(US_Car_df)))==0){
print("there are no na")
}else {
print(" the number of na are",(is.na(sunny)))
}
}

getting a warning:
In if ((is.na(names(US_Car_df))) == 0) { ... :
the condition has length > 1 and only the first element will be used.

Could someone help me identify what I missed please.
 

Sabyasachi Tripathy

Customer
Customer
#### Assignment : print the count of NAs in individual column.
# run a for loop on names(dataframe)


for (sunny in (names(US_Car_df))){
if((is.na(names(US_Car_df)))==0){
print("there are no na")
}else {
print(" the number of na are",(is.na(sunny)))
}
}

getting a warning:
In if ((is.na(names(US_Car_df))) == 0) { ... :
the condition has length > 1 and only the first element will be used.

Could someone help me identify what I missed please.
Hi ,

if((is.na(names(US_Car_df)))==0)

Check this line :

here you should be using the columns one by one . so you should use the variable 'sunny' here .
 

Sunil Kumar_62

Active Member
Hello All,

Can somebody help me with replacing the entire dataset with another dataset for similar codes practice?

for example, we did work on

mtcars$gear

I Want to replace mtcars$gear with Capstone$Glucose.


for the entire code of visualization,


I want to explore capstone$Glucose instead of mtcars$gear.


CAN SOMEBODY GIVE ME THE CODE FOR THIS? I just forgot the code.


While Working on below dataset, I manually changed the dataset names.

upload_2021-1-30_14-45-8.png
 
Hi Friends,
During the coding with simple regression, I got some problems. When I try to correlation I could not get the result even I copied the code given by teacher.
cor (trainingSet $ No_Salesman, trainingSet $ Revenue)
Cor error (trainingSet $ No_Salesman, trainingSet $ Revenue):
'X' must be numeric
I got box plot only for revenue not for could not get for No_salesman.
Regarding with regressor I got different result than teacher with same code. Tripathi sir showed us the result in a nice way short and sweet but I got coef. for all rows of
No_salesman
what about you guys have?
Raj
 

Trapti Jain

Active Member
Hello Guys,
I am facing issue in ggplot, whenever i am writing any code with ggplot , giving the error function not found, can some body please help me for same

ggplot(data=airquality, aes(x=ozone))+ geom_histogram(aes(y=..count..))

> ggplot(data=airquality, aes(x=ozone))+ geom_histogram(aes(y=..count..))
Error in ggplot(data = airquality, aes(x = ozone)) :
could not find function "ggplot"

ggplot(data=airquality, aes(x=ozone))+ geom_histogram(aes(y=..count..)), binwidth=5, color="blue",fill="black")


> ggplot(data=airquality, aes(x=ozone))+ geom_histogram(aes(y=..count..)), binwidth=5, color="blue",fill="black")
Error: unexpected ',' in "ggplot(data=airquality, aes(x=ozone))+ geom_histogram(aes(y=..count..)),"
 
Hello Guys,
I am facing issue in ggplot, whenever i am writing any code with ggplot , giving the error function not found, can some body please help me for same

ggplot(data=airquality, aes(x=ozone))+ geom_histogram(aes(y=..count..))

> ggplot(data=airquality, aes(x=ozone))+ geom_histogram(aes(y=..count..))
Error in ggplot(data = airquality, aes(x = ozone)) :
could not find function "ggplot"

ggplot(data=airquality, aes(x=ozone))+ geom_histogram(aes(y=..count..)), binwidth=5, color="blue",fill="black")


> ggplot(data=airquality, aes(x=ozone))+ geom_histogram(aes(y=..count..)), binwidth=5, color="blue",fill="black")
Error: unexpected ',' in "ggplot(data=airquality, aes(x=ozone))+ geom_histogram(aes(y=..count..)),"
I hope you are executing the library function at the start - library(ggplot2)
 
Top