2.4 Frequency Tables and Sorting Data Frames

Using tally() to Create Frequency Tables

We can use the tally() function to create a frequency table of the Age variable (in the MindsetMatters data frame). This will tell us how many housekeepers there were of each age.

tally(MindsetMatters$Age)

We don't have to use the $ notation. We could also specify the variable and data frame separately, like this:

tally(~ Age, data = MindsetMatters)
Age — counts from tally()
19212223242627282930313233343537
1111434261314211
38394041424344454647485052535455
5223312321311121
5758616265NA
111111

The rows that start with 19, 38, and 57 represent the ages of the housekeepers and the numbers underneath them represent how many of each age are in the data frame. For example, there is one housekeeper who is 19 years old. There are two housekeepers who are 54 years old. There are three housekeepers who are 45 years old.

Try using the tally function to make a frequency table of housekeepers by Condition.

require(coursekata) MindsetMatters <- Lock5withR::MindsetMatters %>% mutate(Condition = factor(Cond, levels = c(1, 0), labels = c("Informed", "Uninformed"))) # Use tally() with the MindsetMatters data frame to create a frequency table of housekeepers by Condition # Use tally() with the MindsetMatters data frame to create a frequency table of housekeepers by Condition tally(~Condition, data = MindsetMatters) # Another solution # tally(MindsetMatters$Condition) ex() %>% check_function("tally") %>% check_result() %>% check_equal()

The output of tally() shows us that there are 41 housekeepers who were in the Informed condition and 34 in the Uninformed condition. Taking a look at this frequency table, we might wonder why there were slightly more housekeepers who were informed that their daily work of cleaning was equivalent to getting adequate exercise.

Using arrange() to Sort a Data Frame

Let’s turn our attention to two variables in the MindsetMatters data frame: Age (the age of the housekeepers, in years, at the start of the study) and Wt (their weight, in pounds, at the start of the study).

We might want to sort the whole data frame MindsetMatters by Age. But now we can’t use the sort() function—that only works with vectors, not with data frames. If we want to sort a whole data frame, we will use a different function, arrange().

The arrange() function works similarly to sort(), except now you have to specify both the name of the data frame and the name of the variable you want to use for sorting the rows.

arrange(MindsetMatters, Age)

Importantly, when you use arrange() to sort on one variable (e.g., Age), the order of the rows (which in this case is housekeepers) will change, but the contents of each row will stay the same.

The printout of MindsetMatters won’t stay arranged by age because we didn’t save our work. In order to save the new ordering, we need to assign the arranged version to an R object. We could assign it back to the existing object (MindsetMatters) or to a new object (e.g., Mindset2, MM2, MM_arrange or any other name you want to make up). If we assign it to the existing object it will revise what's in MindsetMatters to be in the new order. In general, it's a good practice to save a changed data frame to a new R object in case you want to go back to the original version.

Let’s use the assignment operator (<-) to assign the arranged data frame to MM_arrange. See if you can edit the code below to save the version of MindsetMatters that is arranged by Age into MM_arrange. Then print out the first six lines of MM_arrange using head().

require(coursekata) MindsetMatters <- Lock5withR::MindsetMatters %>% mutate(Condition = factor(Cond, levels = c(1, 0), labels = c("Informed", "Uninformed"))) # save MindsetMatters, arranged by Age, to MM_arrange arrange(MindsetMatters, Age) # write code to print out the first 6 rows of MM_arrange # save MindsetMatters, arranged by Age, to MM_arrange MM_arrange <- arrange(MindsetMatters, Age) # write code to print out the first 6 rows of MM_arrange head(MM_arrange) no_save <- "Make sure to both `arrange()` `MindsetMatters` by `Age` *and* save the arranged data frame to `MM_arrange`." ex() %>% { check_object(., "MM_arrange") %>% check_equal(incorrect_msg = no_save) check_function(., "arrange") %>% check_arg("...") %>% check_equal() check_function(., "head") %>% check_result() %>% check_equal() }
Data frame output
row Cond Age Wt Wt2 BMI BMI2 Fat Fat2 WHR WHR2 Syst Syst2 Diast Diast2 Condition
1019123124.219.619.726.6NA0.690.691131177572Uninformed
2021156154.425.925.736.4NA0.780.781161356765Uninformed
3122127124.625.625.234.631.60.740.731101036569Informed
4123161161.426.826.938.137.10.900.861261017464Informed
50249091.816.516.8NANA0.730.73NANA7876Uninformed
6124166169.028.529.041.341.10.880.901141235655Informed

Sorting a Data Frame in Descending Order

The function arrange() can also be used to arrange values in descending order by adding desc() around the variable name.

arrange(MindsetMatters, desc(Age))

Try arranging MindsetMatters by Wt in descending order. Save this to MM_desc. Print a few rows of MM_desc to check out what happened.

require(coursekata) MindsetMatters <- Lock5withR::MindsetMatters %>% mutate(Condition = factor(Cond, levels = c(1, 0), labels = c("Informed", "Uninformed"))) # arrange MindsetMatters by Wt in descending order MM_desc <- # write code to print out a few rows of MM_desc # arrange MindsetMatters by Wt in descending order MM_desc <- arrange(MindsetMatters, desc(Wt)) # write code to print out a few rows of MM_desc head(MM_desc) no_save <- "Did you save the arranged dataset to `MM_desc`?" ex() %>% { check_function(., "desc") %>% check_arg("x") %>% check_equal(eval = FALSE) check_function(., "arrange") %>% check_arg(".data") %>% check_equal(eval = FALSE) check_object(., "MM_desc") %>% check_equal(incorrect_msg = no_save) check_function(., "head") %>% check_arg("x") %>% check_equal() }
Data frame output
row Cond Age Wt Wt2 BMI BMI2 Fat Fat2 WHR WHR2 Syst Syst2 Diast Diast2 Condition
1134196198.233.733.545.744.70.830.81164837357Informed
2139189183.234.634.447.046.70.800.7718515499102Informed
3065187186.234.234.147.3NA0.89NA17618810683Uninformed
4129184182.835.935.744.445.00.890.891201247570Informed
5038183186.434.635.244.242.8NANA1151257072Uninformed
6045182180.033.833.5NA45.60.850.881451419684Uninformed

Responses