a)
First we will create the list of stockpriceA, stockpriceB, stockpriceC and then we will add these lists to myportfolio
Code:
stockpriceA <- list(122.0, 120.5,
191.7, 197.2, 199.5)
stockpriceB <- list(58.6, 56.2, 57.9, 53.1, 54.5)
stockpriceC <- list(102.5, 103.3, 104.5, 103.1, 102.6)
myportfolio <- list(stockpriceA,stockpriceB,stockpriceC)
print(str(myportfolio)) will print the list in string form
List of 3 $ :List of 5 ..$ : num 122 ..$ : num 120 ..$ : num 192 ..$ : num 197 ..$ : num 200 $ :List of 5 ..$ : num 58.6 ..$ : num 56.2 ..$ : num 57.9 ..$ : num 53.1 ..$ : num 54.5 $ :List of 5 ..$ : num 102 ..$ : num 103 ..$ : num 104 ..$ : num 103 ..$ : num 103 NULL
b)
Code:
stockpriceA <- list(122.0, 120.5,
191.7, 197.2, 199.5)
stockpriceB <- list(58.6, 56.2, 57.9, 53.1, 54.5)
stockpriceC <- list(102.5, 103.3, 104.5, 103.1, 102.6)
myportfolio <- list(stockpriceA,stockpriceB,stockpriceC)
newportfolio <- list("Portfolio"= myportfolio, "Apple"=
stockpriceA, "Samsung"= stockpriceB, "Microsoft"= stockpriceC)
print(str(newportfolio)):
List of 4 $ Portfolio:List of 3 ..$ :List of 5 .. ..$ : num 122 .. ..$ : num 120 .. ..$ : num 192 .. ..$ : num 197 .. ..$ : num 200 ..$ :List of 5 .. ..$ : num 58.6 .. ..$ : num 56.2 .. ..$ : num 57.9 .. ..$ : num 53.1 .. ..$ : num 54.5 ..$ :List of 5 .. ..$ : num 102 .. ..$ : num 103 .. ..$ : num 104 .. ..$ : num 103 .. ..$ : num 103 $ Apple :List of 5 ..$ : num 122 ..$ : num 120 ..$ : num 192 ..$ : num 197 ..$ : num 200 $ Samsung :List of 5 ..$ : num 58.6 ..$ : num 56.2 ..$ : num 57.9 ..$ : num 53.1 ..$ : num 54.5 $ Microsoft:List of 5 ..$ : num 102 ..$ : num 103 ..$ : num 104 ..$ : num 103 ..$ : num 103 NULL
c)
first I converted the list to data frames and after that I used correlation among them
dataA <-
data.frame(matrix(unlist(stockpriceA), nrow=length(stockpriceA),
byrow=T))
dataB <- data.frame(matrix(unlist(stockpriceB),
nrow=length(stockpriceB), byrow=T))
dataC <- data.frame(matrix(unlist(stockpriceC),
nrow=length(stockpriceC), byrow=T))
corr <-
list(cor(dataB,dataA),cor(dataC,dataB),cor(dataA,dataC))
print(corr)
d)
newportfolio[length(newportfolio)+1] <- list(corr)
e)
newportfolio[length(newportfolio)] <- NULL
or
newportfolio <- newportfolio[-5] #without 5th element
Code:
stockpriceA <- list(122.0, 120.5,
191.7, 197.2, 199.5)
stockpriceB <- list(58.6, 56.2, 57.9, 53.1, 54.5)
stockpriceC <- list(102.5, 103.3, 104.5, 103.1, 102.6)
myportfolio <- list(stockpriceA,stockpriceB,stockpriceC)
#print(str(myportfolio))
newportfolio <- list("Portfolio"= myportfolio, "Apple"=
stockpriceA, "Samsung"= stockpriceB, "Microsoft"=
stockpriceC)
#print(str(newportfolio))
dataA <- data.frame(matrix(unlist(stockpriceA),
nrow=length(stockpriceA), byrow=T))
dataB <- data.frame(matrix(unlist(stockpriceB),
nrow=length(stockpriceB), byrow=T))
dataC <- data.frame(matrix(unlist(stockpriceC),
nrow=length(stockpriceC), byrow=T))
corr <-
list(cor(dataB,dataA),cor(dataC,dataB),cor(dataA,dataC))
newportfolio[length(newportfolio)+1] <- list("corr"=corr)
#print(newportfolio)
newportfolio <- newportfolio[-5]
#print(newportfolio)
Get Answers For Free
Most questions answered within 1 hours.