Page 19 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 19
Fundamentals of Data Structures
Notes
Case Study Counting Candy
The image in Figure 1 shows a simple counting puzzle. The task is to count how many of
each different type of candy there are in the image.
Figure 1: A counting puzzle. How many candies of each different shape are there? (round,
oval, and long). How many candies have a pattern? How many candies are dark and how
many are light?
Our task is to record the different shapes (round, oval, or long), the different shades (light
or dark), and whether there is a pattern on the candies that we can see in the image. How
can this information be entered into R?
One way to enter data in R is to use the scan() function. This allows us to type data into R
separated by spaces. Once we have entered all of the data, we enter an empty line to
indicate to R that we have finished. We will use this to enter the different shapes:
> shapeNames <- scan(what=”character”)
1: round oval long
4:
Read 3 items
> shapeNames
[1] “round” “oval” “long”
Contd...
12 LOVELY PROFESSIONAL UNIVERSITY