Welcome to the Simplilearn Community

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

Sign Up

Data Science with R | Oct 17 - Nov 22 | Priyanka D

#Assignment1 -how to read 3 rows & 3columns

> x=matrix(1:12,3,4,byrow = TRUE)
> print(x)
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12

> x[c(1:3),c(1:3)]
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 5 6 7
[3,] 9 10 11

#Assignment 3 - Arrays - Check for numbers less than the rows n columns########

> A <- array(1:8, dim = c(2,2,2))
> print(A)
, , 1

[,1] [,2]
[1,] 1 3
[2,] 2 4

, , 2

[,1] [,2]
[1,] 5 7
[2,] 6 8

#Assignment2 : Convert list into vector using UNLIST function


> list_data <- list(c("Red", "Green"), c(21,32,11), TRUE, 51.23, 119.1)
> print(list_data)
[[1]]
[1] "Red" "Green"

[[2]]
[1] 21 32 11

[[3]]
[1] TRUE

[[4]]
[1] 51.23

[[5]]
[1] 119.1

> unlist(list_data)
[1] "Red" "Green" "21" "32" "11" "TRUE" "51.23" "119.1"
 
#converting lists to vectors
# Create lists.
list1 <- list(12:25)
print(list1)

list2 <-list(30:43)
print(list2)

# Convert the lists to vectors.
v1 <- unlist(list1)
v2 <- unlist(list2)

print(v1)
print(v2)

# Now add the vectors
result <- v1+v2
print(result)
 
A=18
B=20
if(B>18){print("more")}else{print("less")} - Ans- more
if(B>18){print("less")}else{print("more")} - Ans- less
Here my question is,
Logically B>18 hence I got answer more, but in second line of code I should also get the answer more, however I got answer less.
Why?
Is it because I use less in the first and more in second, if yes then R fails to understand the logical value of numbers.
Please explain.
 
> list_data <- list(c("Red", "Green"), c(21, 32, 11), TRUE, 51.23, 119.1)
> print(list_data)
[[1]]
[1] "Red" "Green"

[[2]]
[1] 21 32 11

[[3]]
[1] TRUE

[[4]]
[1] 51.23

[[5]]
[1] 119.1

> unlist_data <- unlist(list_data)
> print(unlist_data)
[1] "Red" "Green" "21" "32" "11" "TRUE" "51.23" "119.1"
 
#Read 3 Rows and 3 Columns from a Matrix
mat1 =matrix(1:12,3,4,byrow = TRUE)
> print(mat1)
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
> mat1[c(1:3),c(2:4)]
[,1] [,2] [,3]
[1,] 2 3 4
[2,] 6 7 8
[3,] 10 11 12
 
Assignment to read three vectors:

> v <- 1:10
> print(v)
[1] 1 2 3 4 5 6 7 8 9 10
> v[1:3]
[1] 1 2 3
> v[1,2,3]
Error in v[1, 2, 3] : incorrect number of dimensions
> v[c(1,3,7)]
[1] 1 3 7
 
Assignment : How to read three rows and three columns in a matrix:

> m <- matrix(1:20,4,5,byrow=TRUE)
> print(m)
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 6 7 8 9 10
[3,] 11 12 13 14 15
[4,] 16 17 18 19 20
> m[c(1:3),c(3:5)]
[,1] [,2] [,3]
[1,] 3 4 5
[2,] 8 9 10
[3,] 13 14 15
 
Hi Priyanka,
In the below mentioned code how can I put "gupta" in both the data of the list..??

> a=list("ACHAL","SHIVAM")
> a
[[1]]
[1] "ACHAL"
[[2]]
[1] "SHIVAM"
> a2=unlist(append(a,"GUPTA"))
> a2
[1] "ACHAL" "SHIVAM" "GUPTA"
 

_88881

New Member
Hello,
please help me to make the scripting area appear at the top and console area to appear at the bottom in RStudio. The screenshot attached shows how my Rstuido is currently displaying when l open it.
 

Attachments

  • screenshot.PNG
    screenshot.PNG
    74.9 KB · Views: 3

Sonal_33

Member
Hi Priyanka,
I am doing practice on importing the file. I am setting WD but I am getting error.


setwd("E:/R/Sonal/Import_Export_xls.excel")
Error in setwd("E:/R/Sonal/Import_Export_xls.excel") :
cannot change working directory

But when I am using the second option: I got the output
setwd(choose.dir())
getwd()

[1] "E:/R/Sonal"
> getwd()
[1] "E:/R/Sonal"

What the reason for not running first command.
 

_90116

Member
Hi Priyanka,

I've attached a notepad of my assignment.

-Karl
 

Attachments

  • Assignments_Day3.txt
    1.2 KB · Views: 2

EKTA RANI

Member
##assignment of unlist ##
list_data <- list(c("colour","red") , matrix(c(1:10), 2 , 2 ) )
> print(list_data)[[1]]
[1] "colour" "red"

[[2]]
[,1] [,2]
[1,] 1 3
[2,] 2 4

> unlist(list_data)[1] "colour" "red" "1" "2" "3" "4"
##assignmnet of 3 row and 3 col##
mat1 <- matrix (1:16, nrow=4, ncol=4)
> print(mat1) [,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
> > x <- matrix ( 1:16, 4 , 4, byrow = TRUE)> > print(x) [,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[4,] 13 14 15 16
> > mat1 [c(2,4)][1] 2 4> mat1[c(1:3),c(1:3)] [,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
>
 

_90915

Member
Hello Mam,
i posted my assignment in notepad . please check it let me know the error and correction.
 

Attachments

  • Bharat assignment 24&25Oct-20.txt
    3.6 KB · Views: 0
Please find my assignments in the attached note pad.
#Assignment1 -how to read 3 rows & 3columns
#Assignment2 : Convert list into vector using UNLIST function
###Assignment : library("xlsx") ####
Assignment : Try importing data sets mentioned on the LMS portal = 3 files of each type
 

Attachments

  • Venkat_assigenments.txt
    2.5 KB · Views: 6

priyanka_217

Priyanka Dhoot
Customer
Usage of :
1. this helps in generating sequence of numbers.
2. also helped in quick sum & factorial of sequence of numbers
usage of %in%
1. this helped in checking if an assigned variable or a number is present in a sequence or group of numbers.

#usage of :
1:7
print (1:7) # resulted in numbers 1 to 7 showing step up by 1
add.scope(1:7) # tried to add but resulted in Error
v=c(1:7) # this went correct
v # printed 1 to 7
l=(2:8) # this went correct
l # printed 2 to 8
k=c(2:8) # this went correct
k # printed 2 to 8
mi=(2,8) # this is wrong
mi # this resulted in error

# tried for step 2 but didnt worked
ju = [2](2:10)
ju = 2(2:10)
ju=(2:10)
ju
mu=(2:10,2)
mu=(2:10 [2])

rnu=add.scope(1:10) # gave error
nu = sum(1:10)
nu
hi=factorial(2:4)
hi
sum(1:10)+factorial(2:4)



# usage of %in%
v=c(1:7) # this went correct
7%in%v
(1:4)%in%v
v1 <- 8
v2 <- 12
t <- 1:10
print(v1 %in% t)
print(v2 %in% t)
3%in%(1:10)


This is good..next time onwards just take a snapshot of ur R studio and then share it in a pDF...
 

priyanka_217

Priyanka Dhoot
Customer
myList = list(l1 = c(1, 2, 5, 8),
l2 = c(1, 2, 4),
l3 = c(1, 1, 1, 5, 8, 70, 90))
# Apply unlist R function
x=unlist(myList)
x

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

> x
l11 l12 l13 l14 l21 l22 l23 l31 l32 l33 l34 l35 l36 l37
1 2 5 8 1 2 4 1 1 1 5 8 70 90
this is good...please submit all your assignments in one single PDF from next time....
 

priyanka_217

Priyanka Dhoot
Customer
A=18
B=20
if(B>18){print("more")}else{print("less")} - Ans- more
if(B>18){print("less")}else{print("more")} - Ans- less
Here my question is,
Logically B>18 hence I got answer more, but in second line of code I should also get the answer more, however I got answer less.
Why?
Is it because I use less in the first and more in second, if yes then R fails to understand the logical value of numbers.
Please explain.


R studio on its own cannot interpret less or more... for it less and more has no meaning...it will respond as per the condition given by you... in second statemnet if B>18 ,u have told R to give a output less if this condition is satisfied..so acc you have got the output
 

priyanka_217

Priyanka Dhoot
Customer
Hello,
please help me to make the scripting area appear at the top and console area to appear at the bottom in RStudio. The screenshot attached shows how my Rstuido is currently displaying when l open it.
go to View... show all panes options
 
24.10.2020
a. Multiple Indexing
vector1=c(4,2,1)
vector1[c(1,3)]
Output- 4 1

b. Matrix- Indexing 3 rows and 3 columns
mat1= matrix(1:15, ncol=3, nrw=5, byrow=TRUE)
mat1[c(2,4,5),c(1,2,3)]
Output- 4 5 6
10 11 12
13 14 15
c. Using unlist() function to convert list to vector
.list1= list (c("grace", "valerie"), mat1, vector1)
unlist (.list1, recursive=TRUE, use.names=TRUE)
Output- grace, valerie, 1 4 7 10 13
2 5 8 11 14 3 6
9 12 15 4 2 1

25.10.2020- Assignment- Using dplyr package on "iris" data set- Attached as text document.

.
 

Attachments

  • dplyr.iris.txt
    782 bytes · Views: 1
Doubt
The following is the code I typed to try lapply() function.

.list1= list(c("grace","valerie"), mat1, c(20,30,40))
.list1

list2=lapply(.list1, toupper)
list2
str(list2)

Output
list2=lapply(.list1, toupper)
[1] "GRACE" "VALERIE"
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15"
[1] "20" "30" "40"
str(list2)
List of 3
$ : chr [1:2] "GRACE" "VALERIE"
$ : chr [1:15] "1" "2" "3" "4" ...
$ : chr [1:3] "20" "30" "40"


Q1. You said that lapply() can be used only with lists. Then how can we use it with a vector like you did in the example code you showed in class?

Q2. "toupper" should leave the non alphabets alone. But in the above scenario, it has converted matrix into a vector. How is this possible?
 
2.indexing of 3rows and 3 columns

mat=matrix(1:16,4,4,byrow = TRUE)
mat
mat[c(2,3,4),c(1,2,3)]

output:

[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[4,] 13 14 15 16

> mat[c(2,3,4),c(1,2,3)]
[,1] [,2] [,3]
[1,] 5 6 7
[2,] 9 10 11
[3,] 13 14 15

3.Reading 3rows and 3columns

vec4=c(2,8,96)
vec5=c(12,25,85,42,36)
col.names=c("col1","col2","col3")
row.names=c("row1","row2","row3","row4")
mat.names=c("mat1","mat2")
result=array(c(vec4,vec5),dim = c(4,3,2),dimnames =list(row.names,col.names,mat.names))
result
str(result)
result[c(2,3,4),c(1,2,3),1]

output:
col1 col2 col3
row2 8 85 8
row3 96 42 96
row4 12 36 12
 
I am not able to see data in the .r files downloaded from the provided google drive in my local R Studio. I had raised this issue last time also. Can you please send me the file over my email id as an attachment, that would be helpful.
 
A=18
B=20
if(B>18){print("more")}else{print("less")} - Ans- more
if(B>18){print("less")}else{print("more")} - Ans- less
Here my question is,
Logically B>18 hence I got answer more, but in second line of code I should also get the answer more, however I got answer less.
Why?
Is it because I use less in the first and more in second, if yes then R fails to understand the logical value of numbers.
Please explain.

Please respond to my query.
 

Amit Sahay_1

Member
Alumni
Hi Priyanka,

Please find the assignment on importing xlsx, txt & csv files. Earlier was facing technical challenges in uploading. Hence sharing it now.

Regards,
Amit
 

Attachments

  • Day 4_Assignment.txt
    924 bytes · Views: 0
Assignment3 - Check for numbers less than rows and cols

Tried this for both Matrix and Array. The observation was that the numbers get repeated and populated as shown in the output below:

> n <- matrix(1:3,4,5,byrow=TRUE)
Warning message:
In matrix(1:3, 4, 5, byrow = TRUE) :
data length [3] is not a sub-multiple or multiple of the number of rows [4]
> print(n)
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 1 2
[2,] 3 1 2 3 1
[3,] 2 3 1 2 3
[4,] 1 2 3 1 2
> A <- array(1: 17, dim = c(3,4,2))
> print(A)
, , 1
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
, , 2
[,1] [,2] [,3] [,4]
[1,] 13 16 2 5
[2,] 14 17 3 6
[3,] 15 1 4 7
 
#Assignment2 : Convert list into vector using UNLIST function


> my_list<- list("Alpha","Beta","Gamma")
> print(my_list)
[[1]]
[1] "Alpha"
[[2]]
[1] "Beta"
[[3]]
[1] "Gamma"

> ul<- unlist(my_list)
> print(ul)
[1] "Alpha" "Beta" "Gamma"
>
 

EKTA RANI

Member
mam in exporting the file y it shows error

write_xlsx <- (list(iris_1 = iris, cars_1 = cars, mtcars_1 = mtcars),"mydata.xlsx")
Error: unexpected ',' in "write_xlsx <- (list(iris_1 = iris, cars_1 = cars, mtcars_1 = mtcars),"
 

EKTA RANI

Member
summarise(mtcars,first(vs))
first(vs)
1 0
> summarise(mtcars,first(vs),m=mean(vs))
first(vs) m
1 0 0.4375
> summarise(mtcars,last(carb))
last(carb)
1 2
> summarise(mtcars, (x,n)=qsec)
Error: unexpected ',' in "summarise(mtcars, (x,"
> summarise(mtcars, n=qsec)
n
1 16.46
2 17.02
3 18.61
4 19.44
5 17.02
6 20.22
7 15.84
8 20.00
9 22.90
10 18.30
11 18.90
12 17.40
13 17.60
14 18.00
15 17.98
16 17.82
17 17.42
18 19.47
19 18.52
20 19.90
21 20.01
22 16.87
23 17.30
24 15.41
25 17.05
26 18.90
27 16.70
28 16.90
29 14.50
30 15.50
31 14.60
32 18.60
> summarise(n_distinct(carb))
Error in list2(...) : object 'carb' not found
> summarise(mtcars,n_distinct(carb))
n_distinct(carb)
1 6
> summarise(mtcars,sum(gear))
sum(gear)
1 118
> summarise(mtcars,length(am))
length(am)
1 32
> summaise(mtcars,IQR(mpg))

> summarise(mtcars,IQR(mpg))
IQR(mpg)
1 7.375
 

Amit Sahay_1

Member
Alumni
Hi Priyanka,

barplot(count,main = "Bar Plot",xlab = "disp", ylab = "frequency",
names = c("High", "medium","low"), legend = row.names(count),
col = c("red","blue","green"))

In the above bar plot we are taking displacement in x axis. Does the bar plot takes average displacement for gears 3, 4 & 5 respectively in the X axis.

Regards,
amit
 
Last time you explained the project and you said about plotting the number of complaints in month and day, so my question is, Do we have to plot them in different graphs, or do we have to plot them on the same graph?
Cause I am having trouble plotting them together on the same graph.
 

priyanka_217

Priyanka Dhoot
Customer
Doubt
The following is the code I typed to try lapply() function.

.list1= list(c("grace","valerie"), mat1, c(20,30,40))
.list1

list2=lapply(.list1, toupper)
list2
str(list2)

Output
list2=lapply(.list1, toupper)
[1] "GRACE" "VALERIE"
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15"
[1] "20" "30" "40"
str(list2)
List of 3
$ : chr [1:2] "GRACE" "VALERIE"
$ : chr [1:15] "1" "2" "3" "4" ...
$ : chr [1:3] "20" "30" "40"


Q1. You said that lapply() can be used only with lists. Then how can we use it with a vector like you did in the example code you showed in class?

Q2. "toupper" should leave the non alphabets alone. But in the above scenario, it has converted matrix into a vector. How is this possible?


Ans1 : lapply coverts the answer into a list. When you check the structure it mentions LIST explicitly. It can be applied on Vector,Matrix or list but output format will always be list.
Ans.2 : Toupper will convert every letter into upper case. Whereas your lapply function has converted the Vector & matrix both in list data structure.
 

priyanka_217

Priyanka Dhoot
Customer
I am not able to see data in the .r files downloaded from the provided google drive in my local R Studio. I had raised this issue last time also. Can you please send me the file over my email id as an attachment, that would be helpful.
try to open them in notepad/Wordpad. and then copy paste the same in a new R script.
As per Simplilearn norm, please do not share any personal information on any of the links
 

priyanka_217

Priyanka Dhoot
Customer
mam in exporting the file y it shows error

write_xlsx <- (list(iris_1 = iris, cars_1 = cars, mtcars_1 = mtcars),"mydata.xlsx")
Error: unexpected ',' in "write_xlsx <- (list(iris_1 = iris, cars_1 = cars, mtcars_1 = mtcars),"

@Ekta write_xlsx is a function so please do not use any assignment sign after that. Correct code is :
write_xlsx(list(iris_1 = iris, cars_1 = cars, mtcars_1 = mtcars), "mydata.xlsx")
 
Top