1. (R.4) The Public Health Service studied the effects of smoking on health, in a large sample of representative households. For men and for women in each age group, those who had never smoked were on average somewhat healthier than the current smokers, but the current smokers were on average much healthier than those who had recently stopped smoking. A. Why did they study men and women and different age groups separately? B. The lesson seems to be that you should not start smoking, but once you’ve started, don’t stop. Comment.
Functions in R and RStudio
The input(s) of a function are called argument(s). The output from the function is called the return value. When we provide a function with argument(s) and press Return, we are calling or invoking the function. Later, we will learn how to write our own functions. To find out more about a particular function, you can go to the help tab in RStudio in the lower right panel and search for the function name. You can also just use your favorite search engine online and search for R and a keyword or two, including the name of the function if you know it. The help in R or online is often much more than you really want to read. If you want a brief description, start to type a function and pause. A tooltip will appear that has any functions starting with whatever you’ve typed so far, including a brief description of whichever one is highlighted. If you press return, RStudio will complete typing whichever one of those functions is highlighted and type parentheses for you. Now suppose we want to simulate a die roll. The sample() function will help us do that. The sample function has 4 arguments: x, size, replace, and prob. You can verify this by typing a few letters of the function and waiting for the tooltip to appear. Try it! We’ve used the function c() to combine or concatenate. You put numbers in the parentheses, separated by commas, as inputs or arguments and this function will return you a vector. Try it! > c(1, 2, 3, 4, 5, 6) Assign the vector to y. > y = c(1, 2, 3, 4, 5, 6) According to the tooltip (or help) for the sample() function, the first argument is x, and the function will sample from the elements of x. You can specify the arguments in R explicitly by writing something like: > sample(x=y, size =1) or you can specify based on position. > sample(y, 1) will do exactly the same thing. Try it! Then do this: > sample(size = 1, y) This is the same thing. R sees that you explicitly want the size argument to be 1, then it continues to look for any arguments that you haven’t specified yet, finds y, and interprets that as the next argument that it doesn’t know yet, x. So now you’ve seen several ways to roll a die a single time. Now as exercises try each of these. 1. Simulate two die rolls. (Careful, you’ll need to use the replace argument.) 2. Eight die rolls (you won’t be able to do this unless you are using the replace argument correctly). 3. One die roll, but make it so that each of the faces has an equal chance except that rolling a 6 is twice as likely as anything else. (What does the prob argument do?) 4. Instead of typing c(1, 2, 3, 4, 5, 6), there’s a shorthand notation, 1:6. Use this notation to simulate 20 rolls of a die with values from 1 to 11. You can do it all as one line without assigning anything.
Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 07 Stack and Its Applications