5555. Practice Questions - TOPICS: Dataframes, & | !
55.1 Downloadable version
The questions in this section were originally in Microsoft Word files. I converted those files into the content you see below in this website. I did not have time to thoroughly check that the website content was converted 100% accurately. However, I don’t think there should be problems with the website content. Nevertheless, below are the original MS Word files in case you want to refer to those files instead - again they should contain the same content that you see below in the website.
click here for a downloadable version of the questions and sample answers
The data for tihs assignment appears below. Keep in mind that your answers must work even if the actual values in the data were to be different.
click here to see how the data was generated
Run the following code to generate the data for this assignment. (You don’t need to understand this code in order to successfully complete the assignment).
The code generates a dataframe with random values. You can generate a new dataframe with different values by specifying a different value to the “seed” argument of the function.
# The questions in this assignment refer to a dataframe that contains# information about stocks. You can use the following code to generate some# random data about ficticious stocks which you can use as the data for the# assignment. Note that the actual values in the data will should affect the# code that you write for your answers. However, it does help to have some data# on which to test out your answers.# # Supplying the optional "seed" argument with different values will cause# different data to be generated. Multiple calls to the function # that have the same value for seed will always generate the same# data.generateRandomStockData <-function (numberOfStocks=2000, seed =1) {set.seed(seed) # this will "prime" the random number generator# Generate the tickers getTicker <-function(...) {# Generate tickers numberOfLetters <-sample(2:4, size=1, prob=c(.15, .2, .65) ) tickerLetters <-sample(letters, size=numberOfLetters, replace=TRUE)paste0(tickerLetters, collapse="") } uniqueTickers <-vector()while (length(uniqueTickers) < numberOfStocks){#cat("length=",length(uniqueTickers)) tickers <-c(uniqueTickers, sapply(1:numberOfStocks, getTicker)) uniqueTickers <-unique(tickers) uniqueTickers <- uniqueTickers[1:min(length(uniqueTickers),numberOfStocks)] }# Generate the exchange exchanges <-sample(c("nasdaq","nyse"), size=numberOfStocks, replace=TRUE, prob=c(.6, .4) )# Generate the sector sectors <-sample(c("tech","energy","pharm","consumer"), size=numberOfStocks, replace=TRUE, prob=c(.3, .3, .2, .2) )# Generate the open prices numberOfCheapStocks <-floor(.15* numberOfStocks) numberOf100Stocks <-floor(.40* numberOfStocks) numberOf1000Stocks <-floor(.18* numberOfStocks) numberOfRemainingStocks <- numberOfStocks - numberOfCheapStocks - numberOf100Stocks - numberOf1000Stocks openPrices <-sample(095:105, numberOfCheapStocks, replace=TRUE) /100 openPrices <-c(openPrices, sample(9800:10200, numberOf100Stocks, replace=TRUE) /100) openPrices <-c(openPrices, sample(99000:101000, numberOf1000Stocks, replace=TRUE) /100) openPrices <-c(openPrices, sample(1:200000, numberOfRemainingStocks, replace=TRUE) /100) news <-sample(c(TRUE, FALSE), numberOfStocks, replace=TRUE) closePrices <- openPrices * (1+rnorm(length(openPrices), mean=0, sd=.10)) closePrices <-round(closePrices, 2) df <-data.frame( ticker=uniqueTickers, exchange=exchanges, sector=sectors, open=openPrices, close=closePrices, news=news) df <- df[order(df$ticker),]rownames(df) <-1:nrow(df) df}# Generate the data.stocks <-generateRandomStockData(100)
55.3 The Assignment
Assume that a dataframe named stocks contains information about stocks that traded today. The data is in the format shown below.
Note that what is shown below is just some sample data. The dataframe may actually contain thousands of rows. Answer the questions below by writing the R code that will return the correct result.
The open and close columns represent the opening and closing prices for that day.
The “news” column contains logical TRUE/FALSE values. The value is TRUE if significant news about that stock came out that day (the news might be good news or bad news - you don’t know). If no news came out that day then the value in the “news” column is FALSE.
55.3.1 Sample stock trading data:
The following shows the first 7 rows from the dataframe that was used to generate the answers below. There are actully many more rows in the dataframe.
head(stocks, 7)
ticker exchange sector open close news
1 ac nyse tech 1142.28 1092.00 FALSE
2 azma nyse tech 636.36 676.75 FALSE
3 bd nasdaq energy 1209.54 1097.43 FALSE
4 bj nyse consumer 1.05 1.24 TRUE
5 bkcc nyse tech 100.33 97.99 FALSE
6 bmcj nasdaq energy 251.49 284.03 FALSE
7 bzqa nasdaq energy 1006.36 1081.27 FALSE
HOWEVER, the following answer is WRONG. It’s true that between the two commands all of the data is shown. However, to answer correctly your FINAL COMMAND must SHOW ALL OF THE DATA THAT WAS ASKED FOR:
Write R code that answers each question. Remember that the order of operations for logical operators are ! is first & is second | is third. For some answers, it might be necessary for you to use (parentheses) to override R’s natural order of operations. For example:
Just as in math order of operations is important
2+34 is 14 but (2+3)4 is 20.
Similarly in R
TRUE|FALSE&FALSE is TRUE but (TRUE|FALSE)&FALSE is FALSE
If you are having trouble, it might help for you to review the contents of the Notes files for “dataframes” and for “logical operators”. See the “Notes” folder on Canvas for these and other Notes files.
NOTE : The solutions shown below are just sample solutions. It is possible that other code would also be correct.
Write a command that returns the number (i.e. how many) nasdaq tech stocks there are.
click for answer
sum ( stocks$exchange =="nasdaq")
[1] 53
Show all tech and energy stocks.
click for answer
# One possible answer:stocks [ stocks$sector =="tech"| stocks$sector =="energy" , ]
ticker exchange sector open close news
1 ac nyse tech 1142.28 1092.00 FALSE
2 azma nyse tech 636.36 676.75 FALSE
3 bd nasdaq energy 1209.54 1097.43 FALSE
5 bkcc nyse tech 100.33 97.99 FALSE
6 bmcj nasdaq energy 251.49 284.03 FALSE
7 bzqa nasdaq energy 1006.36 1081.27 FALSE
8 cfj nasdaq energy 1.04 1.07 TRUE
9 ckan nasdaq energy 100.35 109.52 TRUE
11 cof nyse tech 1650.17 1732.12 FALSE
13 cxif nyse energy 99.29 85.35 TRUE
16 diw nasdaq energy 33.29 28.60 FALSE
17 dqj nasdaq tech 995.09 804.12 TRUE
20 ekbt nyse tech 618.93 600.65 FALSE
23 ezzp nasdaq tech 855.71 835.37 TRUE
24 fdc nyse energy 996.48 813.17 FALSE
25 fot nyse energy 0.98 1.06 FALSE
26 fqig nyse tech 98.84 81.43 FALSE
27 ft nasdaq energy 1544.65 1693.83 TRUE
28 ftie nasdaq energy 101.27 111.47 TRUE
29 fyh nasdaq energy 1.00 0.97 FALSE
31 gncs nasdaq tech 98.05 92.12 FALSE
32 goty nyse energy 100.80 92.18 FALSE
33 gqaw nasdaq energy 606.42 598.14 TRUE
34 gsj nasdaq energy 1.03 0.98 FALSE
35 gxu nasdaq energy 99.14 93.84 TRUE
36 hcss nyse energy 1621.41 1462.97 TRUE
37 huej nasdaq tech 1779.68 1673.39 FALSE
38 ik nyse energy 1916.52 1809.68 FALSE
40 ioyf nyse energy 1007.93 972.99 FALSE
41 it nasdaq tech 1609.66 1578.30 FALSE
42 iuau nasdaq tech 98.54 113.07 FALSE
43 iyne nasdaq tech 1.03 1.04 TRUE
44 jggk nyse energy 1007.48 1043.63 TRUE
46 jnjy nasdaq energy 99.81 89.92 TRUE
47 jvn nyse energy 1.00 1.12 TRUE
48 kqdm nasdaq energy 98.10 92.44 FALSE
50 lfh nyse tech 101.59 109.33 TRUE
53 luhd nasdaq tech 101.98 91.97 FALSE
54 mn nyse energy 705.41 691.38 FALSE
58 noq nyse energy 1002.29 935.87 FALSE
59 nqcv nasdaq tech 100.14 107.34 FALSE
62 pkjg nyse energy 100.55 104.55 FALSE
63 poba nasdaq energy 100.42 97.95 TRUE
64 pqy nasdaq energy 1542.45 1671.03 FALSE
67 rahu nasdaq energy 996.00 880.40 TRUE
69 rsau nasdaq energy 0.99 0.92 TRUE
72 sdbp nasdaq tech 100.94 90.59 FALSE
78 tei nyse energy 99.97 114.37 FALSE
79 tgmv nasdaq energy 101.09 105.96 FALSE
80 tt nasdaq energy 992.18 970.45 TRUE
84 vnfa nyse tech 100.31 98.76 FALSE
86 xel nasdaq energy 992.44 1140.14 TRUE
88 xjpu nasdaq energy 99.45 111.95 TRUE
93 yloa nasdaq energy 1.05 0.89 FALSE
94 yneg nyse tech 1008.60 886.27 FALSE
95 ypyw nasdaq tech 100.02 93.76 FALSE
96 ywxf nyse tech 0.98 0.97 TRUE
97 zbbs nasdaq tech 100.87 100.91 TRUE
98 zhue nyse energy 1003.71 978.93 FALSE
99 zw nasdaq energy 1538.76 1654.85 TRUE
# Another possible answerstocks [ stocks$sector %in%c( "tech" , "energy") , ]
ticker exchange sector open close news
1 ac nyse tech 1142.28 1092.00 FALSE
2 azma nyse tech 636.36 676.75 FALSE
3 bd nasdaq energy 1209.54 1097.43 FALSE
5 bkcc nyse tech 100.33 97.99 FALSE
6 bmcj nasdaq energy 251.49 284.03 FALSE
7 bzqa nasdaq energy 1006.36 1081.27 FALSE
8 cfj nasdaq energy 1.04 1.07 TRUE
9 ckan nasdaq energy 100.35 109.52 TRUE
11 cof nyse tech 1650.17 1732.12 FALSE
13 cxif nyse energy 99.29 85.35 TRUE
16 diw nasdaq energy 33.29 28.60 FALSE
17 dqj nasdaq tech 995.09 804.12 TRUE
20 ekbt nyse tech 618.93 600.65 FALSE
23 ezzp nasdaq tech 855.71 835.37 TRUE
24 fdc nyse energy 996.48 813.17 FALSE
25 fot nyse energy 0.98 1.06 FALSE
26 fqig nyse tech 98.84 81.43 FALSE
27 ft nasdaq energy 1544.65 1693.83 TRUE
28 ftie nasdaq energy 101.27 111.47 TRUE
29 fyh nasdaq energy 1.00 0.97 FALSE
31 gncs nasdaq tech 98.05 92.12 FALSE
32 goty nyse energy 100.80 92.18 FALSE
33 gqaw nasdaq energy 606.42 598.14 TRUE
34 gsj nasdaq energy 1.03 0.98 FALSE
35 gxu nasdaq energy 99.14 93.84 TRUE
36 hcss nyse energy 1621.41 1462.97 TRUE
37 huej nasdaq tech 1779.68 1673.39 FALSE
38 ik nyse energy 1916.52 1809.68 FALSE
40 ioyf nyse energy 1007.93 972.99 FALSE
41 it nasdaq tech 1609.66 1578.30 FALSE
42 iuau nasdaq tech 98.54 113.07 FALSE
43 iyne nasdaq tech 1.03 1.04 TRUE
44 jggk nyse energy 1007.48 1043.63 TRUE
46 jnjy nasdaq energy 99.81 89.92 TRUE
47 jvn nyse energy 1.00 1.12 TRUE
48 kqdm nasdaq energy 98.10 92.44 FALSE
50 lfh nyse tech 101.59 109.33 TRUE
53 luhd nasdaq tech 101.98 91.97 FALSE
54 mn nyse energy 705.41 691.38 FALSE
58 noq nyse energy 1002.29 935.87 FALSE
59 nqcv nasdaq tech 100.14 107.34 FALSE
62 pkjg nyse energy 100.55 104.55 FALSE
63 poba nasdaq energy 100.42 97.95 TRUE
64 pqy nasdaq energy 1542.45 1671.03 FALSE
67 rahu nasdaq energy 996.00 880.40 TRUE
69 rsau nasdaq energy 0.99 0.92 TRUE
72 sdbp nasdaq tech 100.94 90.59 FALSE
78 tei nyse energy 99.97 114.37 FALSE
79 tgmv nasdaq energy 101.09 105.96 FALSE
80 tt nasdaq energy 992.18 970.45 TRUE
84 vnfa nyse tech 100.31 98.76 FALSE
86 xel nasdaq energy 992.44 1140.14 TRUE
88 xjpu nasdaq energy 99.45 111.95 TRUE
93 yloa nasdaq energy 1.05 0.89 FALSE
94 yneg nyse tech 1008.60 886.27 FALSE
95 ypyw nasdaq tech 100.02 93.76 FALSE
96 ywxf nyse tech 0.98 0.97 TRUE
97 zbbs nasdaq tech 100.87 100.91 TRUE
98 zhue nyse energy 1003.71 978.93 FALSE
99 zw nasdaq energy 1538.76 1654.85 TRUE
Show all tech and energy stocks on the nasdaq that had news today.
click for answer
# One possible answer:# NOTE that the parentheses in the answer ARE NECESSARY due to the fact that the order of operations# is (1) ! (2) & (3) | # If the parentheses weren't there the command below would return those energy stocks # that had news, and also the "tech" stocks whether they had news or not.stocks [ (stocks$sector =="tech"| stocks$sector =="energy" ) & stocks$news , ]
ticker exchange sector open close news
8 cfj nasdaq energy 1.04 1.07 TRUE
9 ckan nasdaq energy 100.35 109.52 TRUE
13 cxif nyse energy 99.29 85.35 TRUE
17 dqj nasdaq tech 995.09 804.12 TRUE
23 ezzp nasdaq tech 855.71 835.37 TRUE
27 ft nasdaq energy 1544.65 1693.83 TRUE
28 ftie nasdaq energy 101.27 111.47 TRUE
33 gqaw nasdaq energy 606.42 598.14 TRUE
35 gxu nasdaq energy 99.14 93.84 TRUE
36 hcss nyse energy 1621.41 1462.97 TRUE
43 iyne nasdaq tech 1.03 1.04 TRUE
44 jggk nyse energy 1007.48 1043.63 TRUE
46 jnjy nasdaq energy 99.81 89.92 TRUE
47 jvn nyse energy 1.00 1.12 TRUE
50 lfh nyse tech 101.59 109.33 TRUE
63 poba nasdaq energy 100.42 97.95 TRUE
67 rahu nasdaq energy 996.00 880.40 TRUE
69 rsau nasdaq energy 0.99 0.92 TRUE
80 tt nasdaq energy 992.18 970.45 TRUE
86 xel nasdaq energy 992.44 1140.14 TRUE
88 xjpu nasdaq energy 99.45 111.95 TRUE
96 ywxf nyse tech 0.98 0.97 TRUE
97 zbbs nasdaq tech 100.87 100.91 TRUE
99 zw nasdaq energy 1538.76 1654.85 TRUE
# Another possible answer, note that ==TRUE is not necessary but will workstocks [ (stocks$sector =="tech"| stocks$sector =="energy" ) & stocks$news ==TRUE, ]
ticker exchange sector open close news
8 cfj nasdaq energy 1.04 1.07 TRUE
9 ckan nasdaq energy 100.35 109.52 TRUE
13 cxif nyse energy 99.29 85.35 TRUE
17 dqj nasdaq tech 995.09 804.12 TRUE
23 ezzp nasdaq tech 855.71 835.37 TRUE
27 ft nasdaq energy 1544.65 1693.83 TRUE
28 ftie nasdaq energy 101.27 111.47 TRUE
33 gqaw nasdaq energy 606.42 598.14 TRUE
35 gxu nasdaq energy 99.14 93.84 TRUE
36 hcss nyse energy 1621.41 1462.97 TRUE
43 iyne nasdaq tech 1.03 1.04 TRUE
44 jggk nyse energy 1007.48 1043.63 TRUE
46 jnjy nasdaq energy 99.81 89.92 TRUE
47 jvn nyse energy 1.00 1.12 TRUE
50 lfh nyse tech 101.59 109.33 TRUE
63 poba nasdaq energy 100.42 97.95 TRUE
67 rahu nasdaq energy 996.00 880.40 TRUE
69 rsau nasdaq energy 0.99 0.92 TRUE
80 tt nasdaq energy 992.18 970.45 TRUE
86 xel nasdaq energy 992.44 1140.14 TRUE
88 xjpu nasdaq energy 99.45 111.95 TRUE
96 ywxf nyse tech 0.98 0.97 TRUE
97 zbbs nasdaq tech 100.87 100.91 TRUE
99 zw nasdaq energy 1538.76 1654.85 TRUE
Show all nasdaq stocks that went up by at least $10.00.
click for answer
# One possible answer:stocks[ stocks$exchange =="nasdaq"& stocks$close - stocks$open >=10 , ]
ticker exchange sector open close news
6 bmcj nasdaq energy 251.49 284.03 FALSE
7 bzqa nasdaq energy 1006.36 1081.27 FALSE
10 cmiy nasdaq consumer 1004.56 1094.83 TRUE
19 duuv nasdaq consumer 997.98 1010.85 TRUE
27 ft nasdaq energy 1544.65 1693.83 TRUE
28 ftie nasdaq energy 101.27 111.47 TRUE
42 iuau nasdaq tech 98.54 113.07 FALSE
61 phir nasdaq pharm 101.14 116.81 FALSE
64 pqy nasdaq energy 1542.45 1671.03 FALSE
66 qrwo nasdaq consumer 100.49 119.08 FALSE
77 tdma nasdaq pharm 1991.81 2003.91 FALSE
86 xel nasdaq energy 992.44 1140.14 TRUE
88 xjpu nasdaq energy 99.45 111.95 TRUE
99 zw nasdaq energy 1538.76 1654.85 TRUE
Show all nasdaq stocks that went up by at least 10%.
click for answer
# One possible answer:stocks[ stocks$exchange =="nasdaq"& stocks$close >=1.1* stocks$open , ]
ticker exchange sector open close news
6 bmcj nasdaq energy 251.49 284.03 FALSE
28 ftie nasdaq energy 101.27 111.47 TRUE
42 iuau nasdaq tech 98.54 113.07 FALSE
61 phir nasdaq pharm 101.14 116.81 FALSE
66 qrwo nasdaq consumer 100.49 119.08 FALSE
86 xel nasdaq energy 992.44 1140.14 TRUE
88 xjpu nasdaq energy 99.45 111.95 TRUE
Show all nasdaq stocks that went up or down by at least 10%
click for answer
# One possible answer. Note that the parentheses in the answer ARE necessary due to order of # operations of !, &, |. stocks[ stocks$exchange =="nasdaq"& ( stocks$close >=1.1* stocks$open | stocks$close <=0.9* stocks$open ) , ]
ticker exchange sector open close news
6 bmcj nasdaq energy 251.49 284.03 FALSE
14 cywu nasdaq consumer 1931.06 1687.95 TRUE
16 diw nasdaq energy 33.29 28.60 FALSE
17 dqj nasdaq tech 995.09 804.12 TRUE
28 ftie nasdaq energy 101.27 111.47 TRUE
42 iuau nasdaq tech 98.54 113.07 FALSE
61 phir nasdaq pharm 101.14 116.81 FALSE
66 qrwo nasdaq consumer 100.49 119.08 FALSE
67 rahu nasdaq energy 996.00 880.40 TRUE
72 sdbp nasdaq tech 100.94 90.59 FALSE
86 xel nasdaq energy 992.44 1140.14 TRUE
88 xjpu nasdaq energy 99.45 111.95 TRUE
93 yloa nasdaq energy 1.05 0.89 FALSE
# The following, without the parentheses, is NOT CORRECT:# If the parentheses weren't there then nasdaq stocks that we up by 10% would# be shown but stocks that went down by 10% would be shown no matter what exchang e the stock was on.stocks[ stocks$exchange =="nasdaq"& stocks$close >=1.1* stocks$open | stocks$close <=0.9* stocks$open , ]
Show all stocks that had news and/or fluctuated in price by at least $10.00.
click for answer
# One possible answer - any of the following will cause a stock to be displayed:# if the stock had news or if the stock went up by at least $10 or if the stock went down by at least $10stocks [ stocks$news | stocks$close - stocks$open >=10| stocks$open - stocks$close >=10 , ]
Show the average opening price of nasdaq stocks that went up
click for answer
# One possible answer:mean ( stocks [ stocks$exchange =="nasdaq"& stocks$open < stocks$close , "open" ] )
[1] 542.9117
# Another possible answer - this version takes the open prices as a vector and pulls out the data from that# vector based on the values of the other columns in the dataframemean ( stocks$open [ stocks$exchange =="nasdaq"& stocks$open < stocks$close ] )
[1] 542.9117
Show all tech and energy stocks that went up or down by at least 10%
click for answer
# One possible answer - the parentheses ARE necessary due to order of operations.stocks[ (stocks$sector =="tech"| stocks$sector =="energy") & (stocks$close >=1.1* stocks$open | stocks$close <=0.9* stocks$close) , ]
ticker exchange sector open close news
6 bmcj nasdaq energy 251.49 284.03 FALSE
28 ftie nasdaq energy 101.27 111.47 TRUE
42 iuau nasdaq tech 98.54 113.07 FALSE
47 jvn nyse energy 1.00 1.12 TRUE
78 tei nyse energy 99.97 114.37 FALSE
86 xel nasdaq energy 992.44 1140.14 TRUE
88 xjpu nasdaq energy 99.45 111.95 TRUE
Show all tech and energy stocks on the nasdaq that went up by 10% or that had news today.
click for answer
# One possible answer - the parentheses ARE necessary due to order of operations.stocks[ stocks$sector =="nasdaq"& (stocks$sector =="tech"| stocks$sector =="energy") & (stocks$close >=1.1* stocks$open | stocks$news) , ]
[1] ticker exchange sector open close news
<0 rows> (or 0-length row.names)
Show all stocks that were "surprising" in that either they had no news and the price went up or down by at least $10.00 or that had news but the price remained stable and didn't fluctuate up or down by more than $1.00
Your answer should include a single command that when executed displays BOTH types of “surprising” stocks but no other stocks.
click for answer
# One possible answer - the parentheses ARE necessary due to order of operations.stocks[ stocks$news ==FALSE&# no news and (stocks$close - stocks$open >=10| stocks$open - stocks$close >=10 ) # went up or down by at least $10|# OR stocks$news ==TRUE&# yes - had news and abs ( stocks$open - stocks$close) <=1# the price remained stable (abs is the absolute value function) , ] # don't forget the comma
# Another possible answer - no parentheses necessary since all &'s naturally come before | so it will work as is. stocks[ stocks$news ==FALSE&# no news and abs(stocks$open - stocks$close) >=10# went up or down by at least $10 - abs is the absolute value|# OR stocks$news ==TRUE&# yes - had news and abs ( stocks$open - stocks$close) <=1# the price remained stable (abs is the absolute value function) , ] # don't forget the comma
Show the difference between the average price of nasdaq tech stocks closing prices and nasdaq non-tech stock closing prices
click for answer
# one possible answer - this is all one commandmean(stocks[ stocks$exchange =="nasdaq"& stocks$sector =="tech" , "close"]) -mean(stocks[ stocks$exchange =="nasdaq"& stocks$sector !="tech" , "close"])
[1] -53.95524
# another possible answer - this is all one commandmean ( stocks$close[ stocks$exchange =="nasdaq"& stocks$sector =="tech" ] ) -mean ( stocks$close[ stocks$exchange =="nasdaq"& stocks$sector !="tech" ] )
[1] -53.95524
Show how many (i.e. a single number) of nasdaq stocks that opened or closed below $1
click for answer
# one possible answer - the parentheses are necessarysum( stocks$exchange =="nasdaq"& (stocks$open <=1| stocks$close <=1) )
[1] 6
Show all nasdaq stocks that opened or closed below $1 or that opened or closed above $1000
click for answer
# one possible answer - the parentheses are necessarystocks [ stocks$exchange =="nasdaq"& ( stocks$open <1| stocks$closed <1| stocks$open >1000| stocks$close >1000 ) , ]
[1] ticker exchange sector open close news
<0 rows> (or 0-length row.names)
Show tech stocks on the nasdaq that opened or closed below $1 or that opened or closed above $1000
click for answer
# one possible answer - the parentheses are necessarystocks [ stocks$exchange =="nasdaq"& stocks$sector =="tech"& ( stocks$open <1| stocks$closed <1| stocks$open >1000| stocks$close >1000 ) , ]
[1] ticker exchange sector open close news
<0 rows> (or 0-length row.names)
Show tech and fin stocks on the nasdaq that opened or closed below $1 or that opened or closed above $100
click for answer
# one possible answer - all the parentheses are necessarystocks [ stocks$exchange =="nasdaq"& ( stocks$sector =="tech"| stocks$sector =="fin" ) & ( stocks$open <1| stocks$closed <1| stocks$open >1000| stocks$close >1000 ) , ]
[1] ticker exchange sector open close news
<0 rows> (or 0-length row.names)
# another possible answer - all the parentheses are necessarystocks [ stocks$exchange =="nasdaq"& stocks$sector %in%c( "tech" , "fin" ) & ( stocks$open <1| stocks$closed <1| stocks$open >1000| stocks$close >1000 ) , ]
[1] ticker exchange sector open close news
<0 rows> (or 0-length row.names)
Show tech stocks on the nasdaq that had news come out today and also opened or closed below $1 or opened or closed above $1000.
You should provide a single answer that includes a command that returns all the data for stocks that meet the specified conditions but no other stock data .
click for answer
# one possible answer - the parentheses are necessarystocks [ stocks$sector =="tech"& stocks$sector =="nasdaq"& stocks$news & ( stocks$open <1| stocks$close <1| stocks$open >1000| stocks$close >1000 ) , ]
[1] ticker exchange sector open close news
<0 rows> (or 0-length row.names)
Show tech stocks on the nasdaq that opened or closed below $1 or that opened or closed above $1000. In addition, show any stocks that had some news come out today.
click for answer
# one possible answer - the parentheses are necessary - the spacing is just to make it easier to see where the# parentheses match up.stocks [ ( stocks$sector =="tech"& stocks$sector =="nasdaq"& ( stocks$open <1| stocks$close <1| stocks$open >1000| stocks$close >1000 ) ) | stocks$news , ]
Answer both parts - provide separate answers for parts (a) and (b)
Did the nasdaq go up or down? Write a command that results in TRUE if the average closing price of nasdaq stocks is above the average opening price of nasdaq stocks.
click for answer
# one possible answer mean ( stocks$close [ stocks$exchange =="nasdaq" ] ) >mean ( stocks$open [ stocks$exchange =="nasdaq" ] )
[1] FALSE
# another possible answer mean ( stocks [ stocks$exchange =="nasdaq" , "close"] ) >mean ( stocks [ stocks$exchange =="nasdaq", "open" ] )
[1] FALSE
Write a command that shows those stocks that moved in the opposite direction to the nasdaq as a whole.
The answer to part (b) should be a single answer that works whether or not the nasdaq went up or down. If the nasdaq went up then show the stocks that went down. If the nasdaq went down, then show the stocks that went up. If you run the same exact command with data from different days, your R code should give the correct answer no matter whether the nasdaq went up or down that day.
click for answer
# one possible answer - no parentheses are necessary - except for the mean() function callsstocks [ mean ( stocks$close [ stocks$exchange =="nasdaq" ] ) ># These two lines have the answer from part (a)mean ( stocks$open [ stocks$exchange =="nasdaq" ] ) # If this evaluates to TRUE then the nasdaq went UP.& stocks$close < stocks$open # and these stocks went DOWN|mean ( stocks$close [ stocks$exchange =="nasdaq" ] ) <# These two lines have the answer from part (a) but mean ( stocks$open [ stocks$exchange =="nasdaq" ] ) # with a "<" instead of ">" i.e. if this evaluates to TRUE# then the nasdaq went DOWN.& stocks$close > stocks$open # and these stocks went UP , ] # don't forget the comma and the closing bracket.
Show tech stocks whose move was 10% more than the average move for all tech stocks and in the same direction (up or down) of the average move for all tech stocks.
click for answer
# one possible answer - for clarity, I broke this answer up into several commands# the following will be a vector of +1's and -1's , +1 if a stock went up and -1 if a stock went down directionOfStockMove <- (stocks$close - stocks$open) /abs(stocks$close - stocks$open)avgOpenForTech <-mean ( stocks$open [ stocks$sector =="tech" ] )avgCloseForTech <-mean ( stocks$close [ stocks$sector =="tech" ] )# the following will be a single +1 if tech went up and -1 if tech went downdirectionForTech <- (avgCloseForTech - avgOpenForTech) /abs(avgCloseForTech - avgOpenForTech)# the following will be a logical vector, TRUE if a stock moved in the same direction as the tech sector, FALSE otherwisesameDirectionAsTech <- directionOfStockMove == directionForTechmoveForStock <- stocks$close - stocks$open # this vector contains one number for each stockmoveForTech <- avgCloseForTech - avgOpenForTech # this is a single number# the following is the final answer. It shows tech stocks that moved in the same direction as the # tech sector and whose move was at least 10 percent more than the move for the tech sector stocks [ stocks$sector =="tech"& sameDirectionAsTech & moveForStock >=1.1* moveForTech , ]
10% more than the average move for all tech stocks and in the same direction (up or down) of the average move for all tech stocks
or 10% more than the average move for all stocks (regardless of the exchange/sector) and in the same direction as the average move for all stocks
You should provide a single answer that includes a command that returns all the data for stocks that meet the specified conditions but no other stock data .
click for answer
# one possible answer - for clarity, I broke this answer up into several commands########################################################## THE FOLLOWING IS COPIED FROM THE ANSWER TO THE PREVIOUS QUESTION########################################################## the following will be a vector of +1's and -1's , +1 if a stock went up and -1 if a stock went down directionOfStockMove <- (stocks$close - stocks$open) /abs(stocks$close - stocks$open)avgOpenForTech <-mean ( stocks$open [ stocks$sector =="tech" ] )avgCloseForTech <-mean ( stocks$close [ stocks$sector =="tech" ] )# the following will be a single +1 if tech went up and -1 if tech went downdirectionForTech <- (avgCloseForTech - avgOpenForTech) /abs(avgCloseForTech - avgOpenForTech)# the following will be a logical vector, TRUE if a stock moved in the same direction as the tech sector, FALSE otherwisesameDirectionAsTech <- directionOfStockMove == directionForTechmoveForStock <- stocks$close - stocks$open # this vector contains one number for each stockmoveForTech <- avgCloseForTech - avgOpenForTech # this is a single number######################################################################################## UP TO HERE WAS THE SAME AS FOR THE PREVIOUS QUESTION - THE REST FROM HERE DOWN IS NEW#######################################################################################avgOpenForAllStocks <-mean ( stocks$open )avgCloseForAllStocks <-mean ( stocks$close )# the following will be a single +1 if stocks on average went up and -1 if stocks on average went downdirectionForMarket <- (avgCloseForAllStocks - avgOpenForAllStocks) /abs(avgCloseForAllStocks - avgOpenForAllStocks)# the following will be a logical vector, TRUE if a stock moved in the same direction as the tech sector, FALSE otherwisesameDirectionAsMarket <- directionOfStockMove == directionForMarket# the following is the final answerstocks [ stocks$sector =="tech"&# show tech stocks ( # (the rest of the logic is the other side of the & on the previous line) ( sameDirectionAsTech & moveForStock >=1.1* moveForTech ) # moved same dir as tech and 10% more|# or ( sameDirectionAsMarket & moveForStock >=1.1* moveForMarket ) # mved in same dir as Mkt and 10% more ) , ]