**R-exercises**, and kindly contributed to R-bloggers)

In this exercise, we cover some basics on frequency tables. We also briefly look at chi-square test for independence to find relationships of two variables. Before proceeding, it might be helpful to look over the help pages for the `table`

, `summary`

, and `margin.table`

functions.

Answers to the exercises are available here.

If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

**Exercise 1**

use the `attach()`

command to load the trees dataset in R

**Exercise 2**

Use the `table()`

command with the arguments: trees$Height and trees$Volume. This will generate a two-way frequency table. Store this in variable mytable.

**Exercise 3**

If you are familiar with excel pivot tables, then you will know this function. Use the margin.table( ) function to get the Height frequencies summed over Volume

**Exercise 4**

Use the `margin.table( )`

function to get the Volume frequencies summed over Height.

**Exercise 5**

Now use the `table()`

function again but using all the features of the trees dataset, that includes girth, height and volume. This will print out a multidimensional 3 way frequency table.

**Exercise 6**

Suppose you have a variable ‘a’ that stores a second sample of heights of trees.

a=c(70, 65, 63, 72, 80, 83, 66, 75, 80, 75, 79, 76, 76, 69, 75, 74, 85, 8, 71, 63, 78, 80, 74, 72, 77, 81, 82, 80, 86, 80, 87)

Use the cbind() to add the a column to your trees dataset. Store the results back into trees.

**Exercise 7**

Now create a 2 way frequency table between Height and a as the arguments. Store this table in mytable_2.

**Exercise 8**

Use the margin.table() function again from Q3 and get Height frequencies summer over a. What differences do you observe from the results of Q3.

**Exercise 9**

Chi Square test for independance:

a)Print the results of the summary() function on mytable. Note the Chi Square test for independance results and P value

b)Print the results of the summary() function on mytable_2. Note the Chi Square test for independance results and P value.

**Exercise 10**

What did the chi square test for independance help you to see?

**leave a comment**for the author, please follow the link and comment on their blog:

**R-exercises**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...