Welcome to the Simplilearn Community

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

Sign Up

DATA SCIENCE WITH R | Nov 28 - Jan 10 | Sabyasanchi (2020)

Ananya Sharma_1

Active Member
Which version software to be installed; the latest version of R i.e. 4.x.x or the one which is specified in simplilearn's software guide (which is 3.x.x)?
 

Ananya Sharma_1

Active Member
Is this the active forum for the batch of 'DS with R' 27Nov- 10Jan?
Neither anyone is discussing anything here nor replying to the queries..
Please send me the link of the active discussion group of this batch!!
 

Sabyasachi Tripathy

Customer
Customer
Is this the active forum for the batch of 'DS with R' 27Nov- 10Jan?
Neither anyone is discussing anything here nor replying to the queries..
Please send me the link of the active discussion group of this batch!!
Yes this is the link for the given batch . You can post your questions and comments here .
 

Arun Hosh

Active Member
# 1- Extract all the elements whose number of characters is more than 5
for this assignment , I was able to write the piece of code below

assign_vec <- c('Ravi','Satish','Ashutosh','Subhashree','Elizabeth','Vishwakarma')
char_vec <- nchar(assign_vec)
char_vec
for( i in char_vec){
if( i > 5 )
print(i)
}

i got output
[1] 6
[1] 8
[1] 10
[1] 9
[1] 11

plz suggest , how do I relate the corresponding element based on number of char.
I think am doing something wrong in the loop or print statement , plz advise
 

Arun Hosh

Active Member
I found this today ( assignment )

# Extract the third element of the vector without using vec1[3]
vec1 <- c(34,67,23,89,12,56,78,39,40,93,78,120,32,67)
for ( i in 1:length (vec1)){
if ( i == 3 ){
print(vec1)
}
}
 

Arun Hosh

Active Member
I found this today ( assignment ), plz share your thoughts

# Extract the highest values of the vector without using max() function.
vec1 <- c(34,67,23,89,12,56,78,39,40,93,78,120,32,67)
lvec1 <- sort(vec1 , decreasing = FALSE)
lvec1
print(lvec1[length(lvec1)])
 

Pallavi_93

Active Member
assignment 1:
assign_vec <- c('Ravi','Satish','Ashutosh','Subhashree','Elizabeth','Vishwakarma')

# 1- Extract all the elements whose number of characters is more than 5

my code:


assign_vec <- c('Ravi','Satish','Ashutosh','Subhashree','Elizabeth','Vishwakarma')


for(val in assign_vec)
{
if(nchar(val) > 5){

print(val)
}
}

output:
[1] "Satish"
[1] "Ashutosh"
[1] "Subhashree"
[1] "Elizabeth"
[1] "Vishwakarma"
 

Pallavi_93

Active Member
# 2 - If the number of characters is odd , convert all of them to lower else convert them to uppercase .

assign_vec <- c('Ravi','Satish','Ashutosh','Subhashree','Elizabeth','Vishwakarma')

for(val in assign_vec)
{
if(nchar(val)%%2 !=0){

print(tolower(val))
}else {

print(toupper(val))
}
}

output:
[1] "RAVI"
[1] "SATISH"
[1] "ASHUTOSH"
[1] "SUBHASHREE"
[1] "elizabeth"
[1] "vishwakarma"
 

Pallavi_93

Active Member
##Extract all the elments which begin with the character 'R

assign_vec <- c('Ravi','Satish','Ashutosh','Subhashree','Elizabeth','Vishwakarma')

for( val in assign_vec){
val <- toupper(val)
if(substring(val,1,1)=="R")
{
print(val)
}
}
output:
[1] "Ravi"
 

Pallavi_93

Active Member
### Assignment
# Extract the highest values of the vector without using max() function.

vec1 <- c(23,45,123,134,20,36,84,56,28,3,19,0,184)

vec1 <- sort(vec1, decreasing = TRUE)

print(paste('highest value is ',vec1[1]))

output:

highest value is 184
 

Pallavi_93

Active Member
# Extract the third element of the vector without using vec1[3]

vec1 <- c(23,45,140,134,20,36,84,56,28,3,19,0,184)

for(val in vec1)
{
if(match(val,vec1)==3){
print(val)
}
}
output:
140
 

Sabyasachi Tripathy

Customer
Customer
# 1- Extract all the elements whose number of characters is more than 5
for this assignment , I was able to write the piece of code below

assign_vec <- c('Ravi','Satish','Ashutosh','Subhashree','Elizabeth','Vishwakarma')
char_vec <- nchar(assign_vec)
char_vec
for( i in char_vec){
if( i > 5 )
print(i)
}

i got output
[1] 6
[1] 8
[1] 10
[1] 9
[1] 11

plz suggest , how do I relate the corresponding element based on number of char.
I think am doing something wrong in the loop or print statement , plz advise
Try this :

print (assign_vec)

instead of
print(i)
 

Arun Hosh

Active Member
Try this :

print (assign_vec)

instead of
print(i)

i changed the code this way

assign_vec <- c('Ravi','Satish','Ashutosh','Subhashree','Elizabeth','Vishwakarma')
char_vec <- nchar(assign_vec)
char_vec
for( i in char_vec){
if( i > 5 )
print(assign_vec)
}


now o/p is as below
[1] "Ravi" "Satish" "Ashutosh" "Subhashree" "Elizabeth" "Vishwakarma"
[1] "Ravi" "Satish" "Ashutosh" "Subhashree" "Elizabeth" "Vishwakarma"
[1] "Ravi" "Satish" "Ashutosh" "Subhashree" "Elizabeth" "Vishwakarma"
[1] "Ravi" "Satish" "Ashutosh" "Subhashree" "Elizabeth" "Vishwakarma"
[1] "Ravi" "Satish" "Ashutosh" "Subhashree" "Elizabeth" "Vishwakarma"
 
horse_data<- read.csv("horse.csv", stringsAsFactors = FALSE)
grouped <- aggregate(x=horse_data$age, by = list(horse_data$rectal_temp), FUN = sum )
grouped

Error in Summary.factor(1L, na.rm = FALSE) :
‘sum’ not meaningful for factors

I have already used stringsAsFactors = FALSE but still getting error as Factor. What has gone wrong
 

Attachments

  • horse.zip
    8.4 KB · Views: 0
assign_vec <- c('Ravi','Satish','Rshutosh','Subhashre','Elizabeth','Vishwakarma')
b<-c()
d<-c()
e<-c()
for (val in assign_vec){
#Extracting data , which has Character length greater than 5
if(nchar(val)>5){
#print(val)
#b <- c(val)
b <-append(b,val)
}
#Converting to lowercase if it's odd otherwise uppercase
if(nchar(val)%%2 == 1){
d <-append(d,tolower(val))
}else{
d <-append(d,toupper(val))
}
#Extracting character starts with R

if(substr(toupper(val),1,1)==toupper("R")){
e<-append(e,val)
}
}
print(b)
print(d)
print(e)

This works fine guys
 
asai <- c('Ravi','Satish','Ashutosh','Subhashree','Elizabeth','Vishwakarma')
for (val in asai)
if (nchar(val) > 5){print (val)}

[1] "Satish"
[1] "Ashutosh"
[1] "Subhashree"
[1] "Elizabeth"
[1] "Vishwakarma"
 
> asai <- c('Ravi','Satish','Ashutosh','Subhashree','Elizabeth','Vishwakarma', "ujkkkjjy")
> for (val in asai)
+ if (nchar(val)%%2 == 1 ){print(toupper(val))}
[1] "ELIZABETH"
[1] "VISHWAKARMA"



and this for the second case
 
vec1 <- c(34,67,23,89,12,56,78,39,40,93,78,120,32,67)
#max(vec1)
#Printing the maximum number in vector without using max() function
sort(vec1,decreasing = TRUE)[1]



#Printing the 3rd index value without using vec1[3] directly
for (i in 1:length(vec1)){
if(i==3){
print(vec1)
}
}



#Append the fname1 with lname1 with a space
Fname <- c('fname1','fname2', 'fname3')
Lname <- c('lname1','lname2','lname3')
res<-c() #Each time we have to empty the vector, otherwise the value will get append
for (val in 1:length(Fname)){
for (val1 in 1:length(Lname)){

#Matching the index value, if matches it will add the value in the new vector
if(val==val1){
res<- append(res,c(Fname[val],Lname[val1]))

}
}
}
print(res)



#Inserting new value in between without using 'after'
vec <- c(34,77,89,34,7,00,33,22,77,99,18,44,79,45)

vec_1 <- vec[1:3]
vec_2 <- vec[4:length(vec)]
new_value <- 60
result<- c(vec_1,new_value,vec_2)
result


These codes works fine.
 

Pallavi_93

Active Member
#create vector with characters and
#extract the values which are repeated more than once

Did any one written code for this assignment? if so please post the code here.
 

Arun Hosh

Active Member
assignment 1:
assign_vec <- c('Ravi','Satish','Ashutosh','Subhashree','Elizabeth','Vishwakarma')

# 1- Extract all the elements whose number of characters is more than 5

my code:


assign_vec <- c('Ravi','Satish','Ashutosh','Subhashree','Elizabeth','Vishwakarma')


for(val in assign_vec)
{
if(nchar(val) > 5){

print(val)
}
}

output:
[1] "Satish"
[1] "Ashutosh"
[1] "Subhashree"
[1] "Elizabeth"
[1] "Vishwakarma"

Awesome , working fine
 

Arun Hosh

Active Member
assign_vec <- c('Ravi','Satish','Rshutosh','Subhashre','Elizabeth','Vishwakarma')
b<-c()
d<-c()
e<-c()
for (val in assign_vec){
#Extracting data , which has Character length greater than 5
if(nchar(val)>5){
#print(val)
#b <- c(val)
b <-append(b,val)
}
#Converting to lowercase if it's odd otherwise uppercase
if(nchar(val)%%2 == 1){
d <-append(d,tolower(val))
}else{
d <-append(d,toupper(val))
}
#Extracting character starts with R

if(substr(toupper(val),1,1)==toupper("R")){
e<-append(e,val)
}
}
print(b)
print(d)
print(e)

Hi Richard,

thanks for sharing , The code is working fine , plz advise why we use

b<-c()
d<-c()
e<-c()

what is the logic behind this
 

Arun Hosh

Active Member
##Extract all the elments which begin with the character 'R

assign_vec <- c('Ravi','Satish','Ashutosh','Subhashree','Elizabeth','Vishwakarma')

for( val in assign_vec){
val <- toupper(val)
if(substring(val,1,1)=="R")
{
print(val)
}
}
output:
[1] "Ravi"


Hi Pallavi - thanks for sharing ,, I tried it this way... plz check

R_assign_vec <- assign_vec[grepl("^R",assign_vec)]
R_assign_vec
 

Ananya Sharma_1

Active Member
I got the question asking to write R code to find if a no. is prime or not. I wrote the following code but getting error. Please anyone explain me where I'm getting wrong!

num = as.integer(readline(prompt="Enter a number: "))

if(num > 1) {
for(i in 2: (num-1)) {
if ((num %% i) == 0) {
print("Composite No.")
}
else
{print("Prime No.")
}
} if(num == 2){
print("Prime No.")
}
} else {
print('Not Prime')
}
 
Hi Richard,

thanks for sharing , The code is working fine , plz advise why we use

b<-c()
d<-c()
e<-c()

what is the logic behind this


If we don't make the vector empty, it will append the results again and again whenever we runs the code. Try without using it. You will find the difference.
 

Golam MUSTAFA

New Member
# 1- Extract all the elements whose number of characters is more than 5
for this assignment , I was able to write the piece of code below

assign_vec <- c('Ravi','Satish','Ashutosh','Subhashree','Elizabeth','Vishwakarma')
char_vec <- nchar(assign_vec)
char_vec
for( i in char_vec){
if( i > 5 )
print(i)
}

i got output
[1] 6
[1] 8
[1] 10
[1] 9
[1] 11

plz suggest , how do I relate the corresponding element based on number of char.
I think am doing something wrong in the loop or print statement , plz advise

Try this
#####
for (name in assign_vec) {
if (nchar(name)>5){
print(name)
}
}
#####
 
I found this today ( assignment ), plz share your thoughts

# Extract the highest values of the vector without using max() function.
vec1 <- c(34,67,23,89,12,56,78,39,40,93,78,120,32,67)
lvec1 <- sort(vec1 , decreasing = FALSE)
lvec1
print(lvec1[length(lvec1)])

vec1 <- c(34,67,23,89,12,56,78,39,40,93,78,120,32,67)
lvec1 <- sort(vec1, decreasing = TRUE)
lvec1[1]
 

Ananya Sharma_1

Active Member
In the following code;
get_percent <- function(a,b,c,d,e){
result=(a+b+c+d+e)/5
return(result)
}
If w/o using return we get the result, so why we used return function?

Also what's the use of prompt command? (I didn't understand it from the R studio description terminal!)
 

Pallavi_93

Active Member
Hi Pallavi - thanks for sharing ,, I tried it this way... plz check

R_assign_vec <- assign_vec[grepl("^R",assign_vec)]
R_assign_vec

Thanks Arun for sharing, i learnt about grepl() function..

as R is case sensitive we should also consider lowercase and uppercase. before extracting the elements begins with 'R'/'r', its good to convert entire vector to uppercase or lowercase and extract according to case. if i am wrong please correct me

assign_vec <- c('ravi','Satish','Ashutosh','Subhashree','Elizabeth','Vishwakarma')
assign_vec <- assign_vec[grepl("^r",assign_vec)]
assign_vec

output :
"ravi"

assign_vec <- c('ravi','Satish','Ashutosh','Subhashree','Elizabeth','Vishwakarma')
assign_vec <- assign_vec[grepl("^R",assign_vec)]
assign_vec

output :
character(0)

arun , grepl("^R",assign_vec) out put is TRUE FALSE FALSE FALSE FALSE FALSE.
1.can you explain me how assign_vec[TRUE FALSE FALSE FALSE FALSE FALSE] working?
2. please explain me about '^' symbol working
 

Pallavi_93

Active Member
#Append the fname1 with lname1 with a space
Fname <- c('fname1','fname2', 'fname3')
Lname <- c('lname1','lname2','lname3')
res<-c() #Each time we have to empty the vector, otherwise the value will get append
for (val in 1:length(Fname)){
for (val1 in 1:length(Lname)){

#Matching the index value, if matches it will add the value in the new vector
if(val==val1){
res<- append(res,c(Fname[val],Lname[val1]))

}
}
}
print(res)


hi Richard, for the above assignment the out put should be 'fanme1 lname1' 'fname2 lname2' 'fname3 lname'

i tried with below code , getting required output

fname <-c('fname1','fname2','fname3')
lname <- c('lname1','lname2','lname3')

for(j in 1:length(fname)){
for(i1 in 1: length(lname)){
if(j==i1){
print(paste(fname[j],' ',lname[i1]))
}
}
}

But i believe that above written code willl not judge the required output in all cases. i mean, the above code works good if and only if the two vectors length is equal. i tried by adding more elements to first vector and tried to print "element NA" if there is no match in vector2. but i am not able to write the code for it.

Note: if the both vectors length is equal then above written code works good. otherwise it wont print the elements which doesnt have match el in other vector.
 

Pallavi_93

Active Member
Assignment3 :
Fname <- c('fname1','fname2', 'fname3')
Lname <- c('lname1','lname2','lname3')

# 1 - Append the fname1 with lname1 with a space

# fname1 lname1 , fname2 lname2 .....

fname <-c('fname1','fname2','fname3')
lname <- c('lname1','lname2','lname3')
res1 <-c()
res2<-c()

for(j in 1:length(fname)){
for(i1 in 1: length(lname)){
if(j==i1){
res1 <-print(paste(fname[j],' ',lname[i1]))
res2 <- append(res2,res1)

}

}
}
res2
 

Pallavi_93

Active Member
#Assignment :

# - insert a number 60 in between the thrird and fourth
#element without 'after = ' inside append .

vec <- c(34,77,89,34,7,00,33,22,77,99,18,44,79,45)

vec1 <- vec[1:3]
vec2 <- vec[4:length(vec)]
vec1 <-append(vec1,60)
vec <- append(vec1,vec2)
vec
 

Pallavi_93

Active Member
#insert a number after the element '0'
#irrespective of the position of element
vec1 <- c(100,23,34,56,34,0,54,22,43,67,0,21,0,98,76,77,78)

val2 <-c()
for(i in 1:length(vec1))
{
if(vec1==0){
val2<-append(val2,vec1)
val2<-append(val2,50)
}else{
val2<-append(val2,vec1)

}
}

> val2
[1] 100 23 34 56 34 0 50 54 22 43 67 0 50
[14] 21 0 50 98 76 77 78
>
 

Arun Hosh

Active Member
anks Arun for sharing, i learnt about grepl() function..

as R is case sensitive we should also consider lowercase and uppercase. before extracting the elements begins with 'R'/'r', its good to convert entire vector to uppercase or lowercase and extract according to case. if i am wrong please correct me

assign_vec <- c('ravi','Satish','Ashutosh','Subhashree','Elizabeth','Vishwakarma')
assign_vec <- assign_vec[grepl("^r",assign_vec)]
assign_vec

output :
"ravi"

assign_vec <- c('ravi','Satish','Ashutosh','Subhashree','Elizabeth','Vishwakarma')
assign_vec <- assign_vec[grepl("^R",assign_vec)]
assign_vec

output :
character(0)

arun , grepl("^R",assign_vec) out put is TRUE FALSE FALSE FALSE FALSE FALSE.
1.can you explain me how assign_vec[TRUE FALSE FALSE FALSE FALSE FALSE] working?
2. please explain me about '^' symbol working


Hi Pallavi -

Basic R Syntax of grep & grepl:

grep("char", x)
grepl("char", x)


Definitions of grep & grepl:

The grep R function searches for matches of certain character pattern in a vector of character strings and returns the indices that yielded a match.

The grepl R function searches for matches of certain character pattern in a vector of character strings and returns a logical vector indicating which elements of the vector contained a match.

hope this helps
 

Arun Hosh

Active Member
### Assignment # import data from different sheets in excel.

its so simple...

dataframe <- read_excel("Excel_file_name" , sheet = "Sheet1")

or you can mention the same as

1st sheet
dataframe <- read_excel("Excel_file_name" , sheet = 1)

2nd sheet
dataframe <- read_excel("Excel_file_name" , sheet = 2)


just specify the sheet name in the excel..... :)

If you guys know any other way , plz share
 
Last edited:

Arpita Mitra_1

Member
Alumni
### Assignment # import data from different sheets in excel.

its so simple...

dataframe <- read_excel("Excel_file_name" , sheet = "Sheet1")

or you can mention the same as

1st sheet
dataframe <- read_excel("Excel_file_name" , sheet = 1)

2nd sheet
dataframe <- read_excel("Excel_file_name" , sheet = 2)


just specify the sheet name in the excel..... :)

If you guys know any other way , plz share

Hi Arun, I also did this same way bt instead of sheet no. i jst specified sheet name.
mydata <- read.xlsx("file_name", sheetName = "mysheet1")
 
Last edited:
Top