55  55. 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 the questions.

Click here for the answers.

55.2 The data

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.

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
stocks
    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
8      cfj   nasdaq   energy    1.04    1.07  TRUE
9     ckan   nasdaq   energy  100.35  109.52  TRUE
10    cmiy   nasdaq consumer 1004.56 1094.83  TRUE
11     cof     nyse     tech 1650.17 1732.12 FALSE
12      ct     nyse consumer  798.12  886.02  TRUE
13    cxif     nyse   energy   99.29   85.35  TRUE
14    cywu   nasdaq consumer 1931.06 1687.95  TRUE
15    dgab   nasdaq consumer    0.99    0.98  TRUE
16     diw   nasdaq   energy   33.29   28.60 FALSE
17     dqj   nasdaq     tech  995.09  804.12  TRUE
18    dswq     nyse    pharm 1979.86 1922.10 FALSE
19    duuv   nasdaq consumer  997.98 1010.85  TRUE
20    ekbt     nyse     tech  618.93  600.65 FALSE
21    elnf   nasdaq consumer  100.04   90.22 FALSE
22    eyeg     nyse    pharm   98.10  111.04 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
30    giou     nyse consumer    0.99    1.05  TRUE
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
39     iow   nasdaq    pharm  155.50  163.04 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
45     jha     nyse    pharm 1003.85 1060.49  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
49    kzoz     nyse consumer   98.53  123.76  TRUE
50     lfh     nyse     tech  101.59  109.33  TRUE
51    lgiz   nasdaq    pharm  100.49   90.62 FALSE
52    lpam     nyse    pharm  100.12  117.09  TRUE
53    luhd   nasdaq     tech  101.98   91.97 FALSE
54      mn     nyse   energy  705.41  691.38 FALSE
55      mw     nyse consumer 1454.92 1276.36  TRUE
56    nekk     nyse    pharm  528.58  529.57  TRUE
57    nlpl   nasdaq    pharm  990.65  938.60  TRUE
58     noq     nyse   energy 1002.29  935.87 FALSE
59    nqcv   nasdaq     tech  100.14  107.34 FALSE
60    pbhn     nyse consumer  990.60  705.79  TRUE
61    phir   nasdaq    pharm  101.14  116.81 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
65      qi   nasdaq    pharm  101.76  107.25  TRUE
66    qrwo   nasdaq consumer  100.49  119.08 FALSE
67    rahu   nasdaq   energy  996.00  880.40  TRUE
68      rl   nasdaq    pharm  817.82  755.49 FALSE
69    rsau   nasdaq   energy    0.99    0.92  TRUE
70      rx     nyse    pharm   99.38   98.62 FALSE
71     sbj     nyse consumer  100.64  100.93 FALSE
72    sdbp   nasdaq     tech  100.94   90.59 FALSE
73    shfw     nyse consumer  101.09   92.69  TRUE
74      sm   nasdaq consumer  100.95   95.91 FALSE
75      su     nyse consumer  100.57  100.90  TRUE
76     svr     nyse    pharm   98.80  100.76  TRUE
77    tdma   nasdaq    pharm 1991.81 2003.91 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
81    tzly     nyse    pharm    1.05    0.95  TRUE
82    uuvf     nyse    pharm   98.40   97.47 FALSE
83    vhas   nasdaq consumer   98.30   92.15  TRUE
84    vnfa     nyse     tech  100.31   98.76 FALSE
85      wk     nyse consumer    0.95    0.98 FALSE
86     xel   nasdaq   energy  992.44 1140.14  TRUE
87      xg   nasdaq    pharm  995.71  997.18 FALSE
88    xjpu   nasdaq   energy   99.45  111.95  TRUE
89    xnbm   nasdaq    pharm    1.00    1.01 FALSE
90    yfqx     nyse consumer 1573.42 1638.12  TRUE
91      yi     nyse    pharm 1000.88  972.07  TRUE
92    yivs     nyse consumer  170.86  159.29  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
100   zxjo     nyse consumer  101.41  102.30 FALSE

You may use multiple R commands to build up to the final answer, however, the final answer must be executed using a single command. For example:

Sample Question: Show those stocks that are in the consumer sector and also those stocks that are in the pharm sector

The following is a CORRECT answer that takes a single command

stocks [ stocks$sector == "consumer" | stocks$sector == "pharm"  , ]
    ticker exchange   sector    open   close  news
4       bj     nyse consumer    1.05    1.24  TRUE
10    cmiy   nasdaq consumer 1004.56 1094.83  TRUE
12      ct     nyse consumer  798.12  886.02  TRUE
14    cywu   nasdaq consumer 1931.06 1687.95  TRUE
15    dgab   nasdaq consumer    0.99    0.98  TRUE
18    dswq     nyse    pharm 1979.86 1922.10 FALSE
19    duuv   nasdaq consumer  997.98 1010.85  TRUE
21    elnf   nasdaq consumer  100.04   90.22 FALSE
22    eyeg     nyse    pharm   98.10  111.04 FALSE
30    giou     nyse consumer    0.99    1.05  TRUE
39     iow   nasdaq    pharm  155.50  163.04 FALSE
45     jha     nyse    pharm 1003.85 1060.49  TRUE
49    kzoz     nyse consumer   98.53  123.76  TRUE
51    lgiz   nasdaq    pharm  100.49   90.62 FALSE
52    lpam     nyse    pharm  100.12  117.09  TRUE
55      mw     nyse consumer 1454.92 1276.36  TRUE
56    nekk     nyse    pharm  528.58  529.57  TRUE
57    nlpl   nasdaq    pharm  990.65  938.60  TRUE
60    pbhn     nyse consumer  990.60  705.79  TRUE
61    phir   nasdaq    pharm  101.14  116.81 FALSE
65      qi   nasdaq    pharm  101.76  107.25  TRUE
66    qrwo   nasdaq consumer  100.49  119.08 FALSE
68      rl   nasdaq    pharm  817.82  755.49 FALSE
70      rx     nyse    pharm   99.38   98.62 FALSE
71     sbj     nyse consumer  100.64  100.93 FALSE
73    shfw     nyse consumer  101.09   92.69  TRUE
74      sm   nasdaq consumer  100.95   95.91 FALSE
75      su     nyse consumer  100.57  100.90  TRUE
76     svr     nyse    pharm   98.80  100.76  TRUE
77    tdma   nasdaq    pharm 1991.81 2003.91 FALSE
81    tzly     nyse    pharm    1.05    0.95  TRUE
82    uuvf     nyse    pharm   98.40   97.47 FALSE
83    vhas   nasdaq consumer   98.30   92.15  TRUE
85      wk     nyse consumer    0.95    0.98 FALSE
87      xg   nasdaq    pharm  995.71  997.18 FALSE
89    xnbm   nasdaq    pharm    1.00    1.01 FALSE
90    yfqx     nyse consumer 1573.42 1638.12  TRUE
91      yi     nyse    pharm 1000.88  972.07  TRUE
92    yivs     nyse consumer  170.86  159.29  TRUE
100   zxjo     nyse consumer  101.41  102.30 FALSE

The following is also a CORRECT answer but this version uses more than one command:

consumerRows <-  stocks$sector == "consumer"
pharmRows <- stocks$sector == "pharm"
stocks [ consumerRows| pharmRows    ,   ]   # this line returns all of the data
    ticker exchange   sector    open   close  news
4       bj     nyse consumer    1.05    1.24  TRUE
10    cmiy   nasdaq consumer 1004.56 1094.83  TRUE
12      ct     nyse consumer  798.12  886.02  TRUE
14    cywu   nasdaq consumer 1931.06 1687.95  TRUE
15    dgab   nasdaq consumer    0.99    0.98  TRUE
18    dswq     nyse    pharm 1979.86 1922.10 FALSE
19    duuv   nasdaq consumer  997.98 1010.85  TRUE
21    elnf   nasdaq consumer  100.04   90.22 FALSE
22    eyeg     nyse    pharm   98.10  111.04 FALSE
30    giou     nyse consumer    0.99    1.05  TRUE
39     iow   nasdaq    pharm  155.50  163.04 FALSE
45     jha     nyse    pharm 1003.85 1060.49  TRUE
49    kzoz     nyse consumer   98.53  123.76  TRUE
51    lgiz   nasdaq    pharm  100.49   90.62 FALSE
52    lpam     nyse    pharm  100.12  117.09  TRUE
55      mw     nyse consumer 1454.92 1276.36  TRUE
56    nekk     nyse    pharm  528.58  529.57  TRUE
57    nlpl   nasdaq    pharm  990.65  938.60  TRUE
60    pbhn     nyse consumer  990.60  705.79  TRUE
61    phir   nasdaq    pharm  101.14  116.81 FALSE
65      qi   nasdaq    pharm  101.76  107.25  TRUE
66    qrwo   nasdaq consumer  100.49  119.08 FALSE
68      rl   nasdaq    pharm  817.82  755.49 FALSE
70      rx     nyse    pharm   99.38   98.62 FALSE
71     sbj     nyse consumer  100.64  100.93 FALSE
73    shfw     nyse consumer  101.09   92.69  TRUE
74      sm   nasdaq consumer  100.95   95.91 FALSE
75      su     nyse consumer  100.57  100.90  TRUE
76     svr     nyse    pharm   98.80  100.76  TRUE
77    tdma   nasdaq    pharm 1991.81 2003.91 FALSE
81    tzly     nyse    pharm    1.05    0.95  TRUE
82    uuvf     nyse    pharm   98.40   97.47 FALSE
83    vhas   nasdaq consumer   98.30   92.15  TRUE
85      wk     nyse consumer    0.95    0.98 FALSE
87      xg   nasdaq    pharm  995.71  997.18 FALSE
89    xnbm   nasdaq    pharm    1.00    1.01 FALSE
90    yfqx     nyse consumer 1573.42 1638.12  TRUE
91      yi     nyse    pharm 1000.88  972.07  TRUE
92    yivs     nyse consumer  170.86  159.29  TRUE
100   zxjo     nyse consumer  101.41  102.30 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:

stocks [ stocks$sector == "consumer" , ]
    ticker exchange   sector    open   close  news
4       bj     nyse consumer    1.05    1.24  TRUE
10    cmiy   nasdaq consumer 1004.56 1094.83  TRUE
12      ct     nyse consumer  798.12  886.02  TRUE
14    cywu   nasdaq consumer 1931.06 1687.95  TRUE
15    dgab   nasdaq consumer    0.99    0.98  TRUE
19    duuv   nasdaq consumer  997.98 1010.85  TRUE
21    elnf   nasdaq consumer  100.04   90.22 FALSE
30    giou     nyse consumer    0.99    1.05  TRUE
49    kzoz     nyse consumer   98.53  123.76  TRUE
55      mw     nyse consumer 1454.92 1276.36  TRUE
60    pbhn     nyse consumer  990.60  705.79  TRUE
66    qrwo   nasdaq consumer  100.49  119.08 FALSE
71     sbj     nyse consumer  100.64  100.93 FALSE
73    shfw     nyse consumer  101.09   92.69  TRUE
74      sm   nasdaq consumer  100.95   95.91 FALSE
75      su     nyse consumer  100.57  100.90  TRUE
83    vhas   nasdaq consumer   98.30   92.15  TRUE
85      wk     nyse consumer    0.95    0.98 FALSE
90    yfqx     nyse consumer 1573.42 1638.12  TRUE
92    yivs     nyse consumer  170.86  159.29  TRUE
100   zxjo     nyse consumer  101.41  102.30 FALSE
stocks [ stocks$sector == "pharm" , ]
   ticker exchange sector    open   close  news
18   dswq     nyse  pharm 1979.86 1922.10 FALSE
22   eyeg     nyse  pharm   98.10  111.04 FALSE
39    iow   nasdaq  pharm  155.50  163.04 FALSE
45    jha     nyse  pharm 1003.85 1060.49  TRUE
51   lgiz   nasdaq  pharm  100.49   90.62 FALSE
52   lpam     nyse  pharm  100.12  117.09  TRUE
56   nekk     nyse  pharm  528.58  529.57  TRUE
57   nlpl   nasdaq  pharm  990.65  938.60  TRUE
61   phir   nasdaq  pharm  101.14  116.81 FALSE
65     qi   nasdaq  pharm  101.76  107.25  TRUE
68     rl   nasdaq  pharm  817.82  755.49 FALSE
70     rx     nyse  pharm   99.38   98.62 FALSE
76    svr     nyse  pharm   98.80  100.76  TRUE
77   tdma   nasdaq  pharm 1991.81 2003.91 FALSE
81   tzly     nyse  pharm    1.05    0.95  TRUE
82   uuvf     nyse  pharm   98.40   97.47 FALSE
87     xg   nasdaq  pharm  995.71  997.18 FALSE
89   xnbm   nasdaq  pharm    1.00    1.01 FALSE
91     yi     nyse  pharm 1000.88  972.07  TRUE

55.4 Questions (WITH SAMPLE SOLUTIONS)

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.

  1. Show all nasdaq stocks.
stocks [ stocks$exchange == "nasdaq" , ] 
   ticker exchange   sector    open   close  news
3      bd   nasdaq   energy 1209.54 1097.43 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
10   cmiy   nasdaq consumer 1004.56 1094.83  TRUE
14   cywu   nasdaq consumer 1931.06 1687.95  TRUE
15   dgab   nasdaq consumer    0.99    0.98  TRUE
16    diw   nasdaq   energy   33.29   28.60 FALSE
17    dqj   nasdaq     tech  995.09  804.12  TRUE
19   duuv   nasdaq consumer  997.98 1010.85  TRUE
21   elnf   nasdaq consumer  100.04   90.22 FALSE
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
29    fyh   nasdaq   energy    1.00    0.97 FALSE
31   gncs   nasdaq     tech   98.05   92.12 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
37   huej   nasdaq     tech 1779.68 1673.39 FALSE
39    iow   nasdaq    pharm  155.50  163.04 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
46   jnjy   nasdaq   energy   99.81   89.92  TRUE
48   kqdm   nasdaq   energy   98.10   92.44 FALSE
51   lgiz   nasdaq    pharm  100.49   90.62 FALSE
53   luhd   nasdaq     tech  101.98   91.97 FALSE
57   nlpl   nasdaq    pharm  990.65  938.60  TRUE
59   nqcv   nasdaq     tech  100.14  107.34 FALSE
61   phir   nasdaq    pharm  101.14  116.81 FALSE
63   poba   nasdaq   energy  100.42   97.95  TRUE
64    pqy   nasdaq   energy 1542.45 1671.03 FALSE
65     qi   nasdaq    pharm  101.76  107.25  TRUE
66   qrwo   nasdaq consumer  100.49  119.08 FALSE
67   rahu   nasdaq   energy  996.00  880.40  TRUE
68     rl   nasdaq    pharm  817.82  755.49 FALSE
69   rsau   nasdaq   energy    0.99    0.92  TRUE
72   sdbp   nasdaq     tech  100.94   90.59 FALSE
74     sm   nasdaq consumer  100.95   95.91 FALSE
77   tdma   nasdaq    pharm 1991.81 2003.91 FALSE
79   tgmv   nasdaq   energy  101.09  105.96 FALSE
80     tt   nasdaq   energy  992.18  970.45  TRUE
83   vhas   nasdaq consumer   98.30   92.15  TRUE
86    xel   nasdaq   energy  992.44 1140.14  TRUE
87     xg   nasdaq    pharm  995.71  997.18 FALSE
88   xjpu   nasdaq   energy   99.45  111.95  TRUE
89   xnbm   nasdaq    pharm    1.00    1.01 FALSE
93   yloa   nasdaq   energy    1.05    0.89 FALSE
95   ypyw   nasdaq     tech  100.02   93.76 FALSE
97   zbbs   nasdaq     tech  100.87  100.91  TRUE
99     zw   nasdaq   energy 1538.76 1654.85  TRUE
  1. Show all nasdaq stocks that went up in price.
stocks [ stocks$exchange == "nasdaq"  & stocks$close > stocks$open   ,  ] 
   ticker exchange   sector    open   close  news
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
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
39    iow   nasdaq    pharm  155.50  163.04 FALSE
42   iuau   nasdaq     tech   98.54  113.07 FALSE
43   iyne   nasdaq     tech    1.03    1.04  TRUE
59   nqcv   nasdaq     tech  100.14  107.34 FALSE
61   phir   nasdaq    pharm  101.14  116.81 FALSE
64    pqy   nasdaq   energy 1542.45 1671.03 FALSE
65     qi   nasdaq    pharm  101.76  107.25  TRUE
66   qrwo   nasdaq consumer  100.49  119.08 FALSE
77   tdma   nasdaq    pharm 1991.81 2003.91 FALSE
79   tgmv   nasdaq   energy  101.09  105.96 FALSE
86    xel   nasdaq   energy  992.44 1140.14  TRUE
87     xg   nasdaq    pharm  995.71  997.18 FALSE
88   xjpu   nasdaq   energy   99.45  111.95  TRUE
89   xnbm   nasdaq    pharm    1.00    1.01 FALSE
97   zbbs   nasdaq     tech  100.87  100.91  TRUE
99     zw   nasdaq   energy 1538.76 1654.85  TRUE
  1. Write a command that returns the number (i.e. how many) nasdaq tech stocks there are.
sum ( stocks$exchange == "nasdaq")
[1] 53
  1. Show all tech and energy stocks.
# 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 answer
stocks [ 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
  1. Show all tech and energy stocks on the nasdaq that had news today.
# 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 work
stocks [  (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
  1. Show all nasdaq stocks that went up by at least $10.00.
# 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
  1. Show all nasdaq stocks that went up by at least 10%.
# 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
  1. Show all nasdaq stocks that went up or down by at least 10%
# 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   ,    ]
   ticker exchange   sector    open   close  news
6    bmcj   nasdaq   energy  251.49  284.03 FALSE
13   cxif     nyse   energy   99.29   85.35  TRUE
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
24    fdc     nyse   energy  996.48  813.17 FALSE
26   fqig     nyse     tech   98.84   81.43 FALSE
28   ftie   nasdaq   energy  101.27  111.47  TRUE
42   iuau   nasdaq     tech   98.54  113.07 FALSE
55     mw     nyse consumer 1454.92 1276.36  TRUE
60   pbhn     nyse consumer  990.60  705.79  TRUE
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
94   yneg     nyse     tech 1008.60  886.27 FALSE
  1. Show all stocks that had news and/or fluctuated in price by at least $10.00.
# 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 $10
stocks [ stocks$news   |  stocks$close - stocks$open >= 10  |  stocks$open - stocks$close >= 10   ,   ]
   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
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
10   cmiy   nasdaq consumer 1004.56 1094.83  TRUE
11    cof     nyse     tech 1650.17 1732.12 FALSE
12     ct     nyse consumer  798.12  886.02  TRUE
13   cxif     nyse   energy   99.29   85.35  TRUE
14   cywu   nasdaq consumer 1931.06 1687.95  TRUE
15   dgab   nasdaq consumer    0.99    0.98  TRUE
17    dqj   nasdaq     tech  995.09  804.12  TRUE
18   dswq     nyse    pharm 1979.86 1922.10 FALSE
19   duuv   nasdaq consumer  997.98 1010.85  TRUE
20   ekbt     nyse     tech  618.93  600.65 FALSE
22   eyeg     nyse    pharm   98.10  111.04 FALSE
23   ezzp   nasdaq     tech  855.71  835.37  TRUE
24    fdc     nyse   energy  996.48  813.17 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
30   giou     nyse consumer    0.99    1.05  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
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
45    jha     nyse    pharm 1003.85 1060.49  TRUE
46   jnjy   nasdaq   energy   99.81   89.92  TRUE
47    jvn     nyse   energy    1.00    1.12  TRUE
49   kzoz     nyse consumer   98.53  123.76  TRUE
50    lfh     nyse     tech  101.59  109.33  TRUE
52   lpam     nyse    pharm  100.12  117.09  TRUE
53   luhd   nasdaq     tech  101.98   91.97 FALSE
54     mn     nyse   energy  705.41  691.38 FALSE
55     mw     nyse consumer 1454.92 1276.36  TRUE
56   nekk     nyse    pharm  528.58  529.57  TRUE
57   nlpl   nasdaq    pharm  990.65  938.60  TRUE
58    noq     nyse   energy 1002.29  935.87 FALSE
60   pbhn     nyse consumer  990.60  705.79  TRUE
61   phir   nasdaq    pharm  101.14  116.81 FALSE
63   poba   nasdaq   energy  100.42   97.95  TRUE
64    pqy   nasdaq   energy 1542.45 1671.03 FALSE
65     qi   nasdaq    pharm  101.76  107.25  TRUE
66   qrwo   nasdaq consumer  100.49  119.08 FALSE
67   rahu   nasdaq   energy  996.00  880.40  TRUE
68     rl   nasdaq    pharm  817.82  755.49 FALSE
69   rsau   nasdaq   energy    0.99    0.92  TRUE
72   sdbp   nasdaq     tech  100.94   90.59 FALSE
73   shfw     nyse consumer  101.09   92.69  TRUE
75     su     nyse consumer  100.57  100.90  TRUE
76    svr     nyse    pharm   98.80  100.76  TRUE
77   tdma   nasdaq    pharm 1991.81 2003.91 FALSE
78    tei     nyse   energy   99.97  114.37 FALSE
80     tt   nasdaq   energy  992.18  970.45  TRUE
81   tzly     nyse    pharm    1.05    0.95  TRUE
83   vhas   nasdaq consumer   98.30   92.15  TRUE
86    xel   nasdaq   energy  992.44 1140.14  TRUE
88   xjpu   nasdaq   energy   99.45  111.95  TRUE
90   yfqx     nyse consumer 1573.42 1638.12  TRUE
91     yi     nyse    pharm 1000.88  972.07  TRUE
92   yivs     nyse consumer  170.86  159.29  TRUE
94   yneg     nyse     tech 1008.60  886.27 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
  1. Show the average opening price of nasdaq stocks that went up
# 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 dataframe
mean   (  stocks$open [  stocks$exchange == "nasdaq" & stocks$open < stocks$close  ]   ) 
[1] 542.9117
  1. Show all tech and energy stocks that went up or down by at least 10%
# 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
  1. Show all tech and energy stocks on the nasdaq that went up by 10% or that had news today.
# 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)
  1. 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.

# 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
   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
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
11    cof     nyse     tech 1650.17 1732.12 FALSE
15   dgab   nasdaq consumer    0.99    0.98  TRUE
18   dswq     nyse    pharm 1979.86 1922.10 FALSE
20   ekbt     nyse     tech  618.93  600.65 FALSE
22   eyeg     nyse    pharm   98.10  111.04 FALSE
24    fdc     nyse   energy  996.48  813.17 FALSE
26   fqig     nyse     tech   98.84   81.43 FALSE
30   giou     nyse consumer    0.99    1.05  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
47    jvn     nyse   energy    1.00    1.12  TRUE
53   luhd   nasdaq     tech  101.98   91.97 FALSE
54     mn     nyse   energy  705.41  691.38 FALSE
56   nekk     nyse    pharm  528.58  529.57  TRUE
58    noq     nyse   energy 1002.29  935.87 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
68     rl   nasdaq    pharm  817.82  755.49 FALSE
69   rsau   nasdaq   energy    0.99    0.92  TRUE
72   sdbp   nasdaq     tech  100.94   90.59 FALSE
75     su     nyse consumer  100.57  100.90  TRUE
77   tdma   nasdaq    pharm 1991.81 2003.91 FALSE
78    tei     nyse   energy   99.97  114.37 FALSE
81   tzly     nyse    pharm    1.05    0.95  TRUE
94   yneg     nyse     tech 1008.60  886.27 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
# 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
   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
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
11    cof     nyse     tech 1650.17 1732.12 FALSE
15   dgab   nasdaq consumer    0.99    0.98  TRUE
18   dswq     nyse    pharm 1979.86 1922.10 FALSE
20   ekbt     nyse     tech  618.93  600.65 FALSE
22   eyeg     nyse    pharm   98.10  111.04 FALSE
24    fdc     nyse   energy  996.48  813.17 FALSE
26   fqig     nyse     tech   98.84   81.43 FALSE
30   giou     nyse consumer    0.99    1.05  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
47    jvn     nyse   energy    1.00    1.12  TRUE
53   luhd   nasdaq     tech  101.98   91.97 FALSE
54     mn     nyse   energy  705.41  691.38 FALSE
56   nekk     nyse    pharm  528.58  529.57  TRUE
58    noq     nyse   energy 1002.29  935.87 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
68     rl   nasdaq    pharm  817.82  755.49 FALSE
69   rsau   nasdaq   energy    0.99    0.92  TRUE
72   sdbp   nasdaq     tech  100.94   90.59 FALSE
75     su     nyse consumer  100.57  100.90  TRUE
77   tdma   nasdaq    pharm 1991.81 2003.91 FALSE
78    tei     nyse   energy   99.97  114.37 FALSE
81   tzly     nyse    pharm    1.05    0.95  TRUE
94   yneg     nyse     tech 1008.60  886.27 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
  1. Show the difference between the average price of nasdaq tech stocks closing prices and nasdaq non-tech stock closing prices
# one possible answer - this is all one command
mean(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 command
mean ( stocks$close[ stocks$exchange == "nasdaq" & stocks$sector == "tech" ] )   -
            mean ( stocks$close[ stocks$exchange == "nasdaq" & stocks$sector != "tech" ] )   
[1] -53.95524
  1. Show how many (i.e. a single number) of nasdaq stocks that opened or closed below $1
# one possible answer - the parentheses are necessary
sum(    stocks$exchange == "nasdaq"  &  (stocks$open <= 1   |  stocks$close <= 1)   )
[1] 6
  1. Show all nasdaq stocks that opened or closed below $1 or that opened or closed above $1000
# one possible answer - the parentheses are necessary
stocks [ 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)
  1. Show tech stocks on the nasdaq that opened or closed below $1 or that opened or closed above $1000
# one possible answer - the parentheses are necessary
stocks [ 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)
  1. Show tech and fin stocks on the nasdaq that opened or closed below $1 or that opened or closed above $100
# one possible answer - all the parentheses are necessary
stocks [ 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 necessary
stocks [ 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)
  1. 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 .

# one possible answer -  the parentheses are necessary
stocks [ 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)
  1. 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.
# 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   ,  ]
   ticker exchange   sector    open   close news
4      bj     nyse consumer    1.05    1.24 TRUE
8     cfj   nasdaq   energy    1.04    1.07 TRUE
9    ckan   nasdaq   energy  100.35  109.52 TRUE
10   cmiy   nasdaq consumer 1004.56 1094.83 TRUE
12     ct     nyse consumer  798.12  886.02 TRUE
13   cxif     nyse   energy   99.29   85.35 TRUE
14   cywu   nasdaq consumer 1931.06 1687.95 TRUE
15   dgab   nasdaq consumer    0.99    0.98 TRUE
17    dqj   nasdaq     tech  995.09  804.12 TRUE
19   duuv   nasdaq consumer  997.98 1010.85 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
30   giou     nyse consumer    0.99    1.05 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
45    jha     nyse    pharm 1003.85 1060.49 TRUE
46   jnjy   nasdaq   energy   99.81   89.92 TRUE
47    jvn     nyse   energy    1.00    1.12 TRUE
49   kzoz     nyse consumer   98.53  123.76 TRUE
50    lfh     nyse     tech  101.59  109.33 TRUE
52   lpam     nyse    pharm  100.12  117.09 TRUE
55     mw     nyse consumer 1454.92 1276.36 TRUE
56   nekk     nyse    pharm  528.58  529.57 TRUE
57   nlpl   nasdaq    pharm  990.65  938.60 TRUE
60   pbhn     nyse consumer  990.60  705.79 TRUE
63   poba   nasdaq   energy  100.42   97.95 TRUE
65     qi   nasdaq    pharm  101.76  107.25 TRUE
67   rahu   nasdaq   energy  996.00  880.40 TRUE
69   rsau   nasdaq   energy    0.99    0.92 TRUE
73   shfw     nyse consumer  101.09   92.69 TRUE
75     su     nyse consumer  100.57  100.90 TRUE
76    svr     nyse    pharm   98.80  100.76 TRUE
80     tt   nasdaq   energy  992.18  970.45 TRUE
81   tzly     nyse    pharm    1.05    0.95 TRUE
83   vhas   nasdaq consumer   98.30   92.15 TRUE
86    xel   nasdaq   energy  992.44 1140.14 TRUE
88   xjpu   nasdaq   energy   99.45  111.95 TRUE
90   yfqx     nyse consumer 1573.42 1638.12 TRUE
91     yi     nyse    pharm 1000.88  972.07 TRUE
92   yivs     nyse consumer  170.86  159.29 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
  1. Answer both parts - provide separate answers for parts (a) and (b)
  1. 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.
# 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
  1. 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.

# one possible answer  -  no parentheses are necessary - except for the mean() function calls
stocks [  
   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.
    ticker exchange   sector    open   close  news
2     azma     nyse     tech  636.36  676.75 FALSE
4       bj     nyse consumer    1.05    1.24  TRUE
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
10    cmiy   nasdaq consumer 1004.56 1094.83  TRUE
11     cof     nyse     tech 1650.17 1732.12 FALSE
12      ct     nyse consumer  798.12  886.02  TRUE
19    duuv   nasdaq consumer  997.98 1010.85  TRUE
22    eyeg     nyse    pharm   98.10  111.04 FALSE
25     fot     nyse   energy    0.98    1.06 FALSE
27      ft   nasdaq   energy 1544.65 1693.83  TRUE
28    ftie   nasdaq   energy  101.27  111.47  TRUE
30    giou     nyse consumer    0.99    1.05  TRUE
39     iow   nasdaq    pharm  155.50  163.04 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
45     jha     nyse    pharm 1003.85 1060.49  TRUE
47     jvn     nyse   energy    1.00    1.12  TRUE
49    kzoz     nyse consumer   98.53  123.76  TRUE
50     lfh     nyse     tech  101.59  109.33  TRUE
52    lpam     nyse    pharm  100.12  117.09  TRUE
56    nekk     nyse    pharm  528.58  529.57  TRUE
59    nqcv   nasdaq     tech  100.14  107.34 FALSE
61    phir   nasdaq    pharm  101.14  116.81 FALSE
62    pkjg     nyse   energy  100.55  104.55 FALSE
64     pqy   nasdaq   energy 1542.45 1671.03 FALSE
65      qi   nasdaq    pharm  101.76  107.25  TRUE
66    qrwo   nasdaq consumer  100.49  119.08 FALSE
71     sbj     nyse consumer  100.64  100.93 FALSE
75      su     nyse consumer  100.57  100.90  TRUE
76     svr     nyse    pharm   98.80  100.76  TRUE
77    tdma   nasdaq    pharm 1991.81 2003.91 FALSE
78     tei     nyse   energy   99.97  114.37 FALSE
79    tgmv   nasdaq   energy  101.09  105.96 FALSE
85      wk     nyse consumer    0.95    0.98 FALSE
86     xel   nasdaq   energy  992.44 1140.14  TRUE
87      xg   nasdaq    pharm  995.71  997.18 FALSE
88    xjpu   nasdaq   energy   99.45  111.95  TRUE
89    xnbm   nasdaq    pharm    1.00    1.01 FALSE
90    yfqx     nyse consumer 1573.42 1638.12  TRUE
97    zbbs   nasdaq     tech  100.87  100.91  TRUE
99      zw   nasdaq   energy 1538.76 1654.85  TRUE
100   zxjo     nyse consumer  101.41  102.30 FALSE
  1. 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.
# 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 down
directionForTech <- (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 otherwise
sameDirectionAsTech <- directionOfStockMove == directionForTech

moveForStock <- stocks$close - stocks$open                    # this vector contains one number for each stock
moveForTech <- 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  ,    ]
   ticker exchange sector   open  close  news
5    bkcc     nyse   tech 100.33  97.99 FALSE
20   ekbt     nyse   tech 618.93 600.65 FALSE
23   ezzp   nasdaq   tech 855.71 835.37  TRUE
26   fqig     nyse   tech  98.84  81.43 FALSE
31   gncs   nasdaq   tech  98.05  92.12 FALSE
53   luhd   nasdaq   tech 101.98  91.97 FALSE
72   sdbp   nasdaq   tech 100.94  90.59 FALSE
84   vnfa     nyse   tech 100.31  98.76 FALSE
95   ypyw   nasdaq   tech 100.02  93.76 FALSE
96   ywxf     nyse   tech   0.98   0.97  TRUE
  1. 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

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 .

# 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 down
directionForTech <- (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 otherwise
sameDirectionAsTech <- directionOfStockMove == directionForTech

moveForStock <- stocks$close - stocks$open                    # this vector contains one number for each stock
moveForTech <- 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 down
directionForMarket <- (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 otherwise
sameDirectionAsMarket <- directionOfStockMove == directionForMarket


# the following is the final answer
stocks [ 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
              )
            ,    ]
Error: object 'moveForMarket' not found

© 2025 Y. Rosenthal. All rights reserved.