Welcome to the first Assignment! Try to solve the following exercises, and if you do not remember how to do code certain things, have a look again at the previous scripts. Programming is about learning by doing! So, do not hesitate to look for help in the internet, too. Once you finished the exercises, compare your results to the provided solutions. Remember that there is almost always more than just one way to solve a problem in R, so as long as you get to the right result, it should not matter too much if your code deviates from the provided solution.
Load “Demographics_Uzbekistan_Worldbank2.csv” into R and convert the first column to rownames.
Select only the entries corresponding to the years 2000 to 2010.
From that, create a table that only contains information about females.
From that, delete everything that is not percentage information, and the rows that say “% of total”.
Create a table with the lower and upper age limits of the remaining age classes.
Transpose the table about female population classes and shorten the column names.
What is the percentage of females older than 44 years for the years 2000 to 2010?
What was the mean percentage of females aged 35-39 between 2000 and 2005, and between 2000 and 2010?
Go back to the first table and create a new table with all information on people aged 0-14.
What was the net change in numbers of male and female children between 1960 and 2016?
Create a vector x that consists of the numbers from 1 to 20. Run a FOR-loop for i in 1 to 20, and at each step, print the product of the value i and value i+1. Then, avoid the last printed value, which is “NA”, by transforming the previous FOR-loop into a WHILE-loop.
Create a vector y that is a sequence from 10 to 200, consisting of values divisible by 10. Run a FOR-loop for i in 1 to 20, and at each iteration, print y[i] if y[i] is divisible by 50, and print “not divisible by 50” if it is not. Do that by including an IF-statement in the FOR-loop. Then, change the IF-statement so only values that are above 50 and below 150 are printed.
Create a function “sum_mean” that uses two vectors a and b as arguments, calculates the sum of both vectors “vectors_sum” and returns the mean value of “vectors_sum”. Run the function with the vectors x and y from the previous two exercises
Write a FOR-loop that computes a vector of all prime numbers between 3 and 100.
Write a function that computes the binomial coefficient of two values n and k, and test it for n=49 and k=6.
For the following exercise, we will work with the built-in dataset iris. You can view a description of this dataset [here] (https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html) and have a look at the data by executing “head(iris)”.
Create a copy of “iris”" that you name “data_iris”. Create a new column in “data_iris” and name it “description”. Characterize each entry according to whether “Sepal.Length”" and “Petal.Length” is less/equal to, or greater than 5.8, and less/equal to, or greater than 4.35, respectively, by writing into the fourth column one of the following four statements:
Write a function that takes (1) a table with the same structure as the iris data and (2) a threshold value as inputs and computes the mean value for “Sepal.Length”" of all entries whose value for “Sepal.Width”" exceeds the threshold value. Run this function on “data_iris”, setting the threshold to 3.