R&M Richards Sleeveless Beaded V Neck Dress And Jacket
a second key programmingtool is the for loop. a for loop is a structure usedto execute a set of code repeatedly. the for loop statement specifieris an index over which the loop is computed. for example, here i'll executea for loop using an index called i. the object, i, will start theloop by taking the first value in the 1 through 10 vector.
that is, to start,i will equal 1. next, the for loopwill execute. then i will take the next valuein this vector, which is 2, and loop willexecute again. this will continue until i hastaken the last value in the vector, 10, and the codeexecutes one last time. in this set of code, the valuei squared will be appended to the vector, x, using theappend function. here i got an error.
i'll take a closer look. i can see that x never actuallyexisted, so there was no way to append anything ontothe first iteration. to fix this, i'll justinitialize x as an empty vector using the concatenatefunction, but leaving the arguments empty. ok, this runs well. look at x. in each iteration the valueof i squared was appended
to the end of x. so the first element was createdwhen i was 1, the second when i was2, and so on. ok, i've done somethingpretty cool here. i've done 10 calculationsusing a for loop, and it wouldn't be hard todo many more with the same set of code. for example, i could easily gofrom 1 to 100, rather than just 1 to 10.
while there are other, betterways to do this particular calculation, there are instanceswhere for loop is very useful. all right, one more lookat the stock data. to calculate the smallest andlargest values for each stock in the stock data set, i'm goingto start by creating an object called the.tickers thatis just a list of the unique stock tickers in the data. since for loops can iterate overany vector, i will write
a for loop to iterate over theobject that i've called the.tickers. it's sometimes helpful to alsogive a meaningful name to the index, so i'm going to changethe index i to ticker. now i need to create codefor the general case. for a given ticker, calculateboth the low and the high value. i'll start by identifying whichrows are of interest in the stocks data set.
the vector called look.at isa boolean vector indicating which observations representthe.ticker for the given iteration. next, i can create twostatements to calculate the lowest low and highest highof these observations. finally, i need to storethese values somehow. i can start by initializing twoobjects, lows and highs. next, i can use an appendcommand to append a value on to the end of the vector.
all right, i can run the codeand print the results, but something's wrong. while i'd want to spot checksome of my data anyways, something bad has happened. a value of na in r means that avalue is missing, and, more generally, oftentimes functionswill return na if any of the observationsare missing. if i took a look through ourdata set, i'd find that there are several observationswith missing na values.
here, i've checked how manyentries in the column low of the stocks data setare missing. in many functions, such as minand max, there's an optional extra argument that is usefulfor ignoring missing data, the na.rm argument, which iwill set to true in the min and max functions. now when i re-run the code,i get sensible results. i'd want to look at the datamore carefully to see why some observations are missing, buti'll leave this as a topic for
another set of videos. one final word-- even in this example, there areother, better functions that could have been usedto get the same results much more quickly. this would be important forcode that should be implemented efficiently, andi'll get to these functions in the future. however, for the beginning rprogrammer, it's sometimes
easier and clearer to simplyimplement a for loop.
Tidak ada komentar:
Posting Komentar