0
votes

I have to make a table in which the mean and median from the logsales per month are set out from the "txhousing" dataset. The exercise i got is the following: “The table below show the means and medians of the log of sales per month, sorted by mean”

  • Insert a new r chunk and type the code in it to display that table
  • Use na.omit to get rid of cases with missing values
  • Use the dplyr command mutate to make the variable logsales
  • Use the dplyr command group_by to group by month
  • Use the dplyr command summarise to display the table
  • Use the dplyr command arrange to sorth by mean
  • Connect the commands with the pipe operator %>%

I've tried to mix up the code multiple times but I can't find out why it keeps on giving me NA's in my table.

library(tidyverse)
summary(txhousing)    
na.omit(txhousing)    
txhousing<- as.data.frame(txhousing)
logsales <- log(txhousing$sales)  
group_by(txhousing, txhousing$month)   
txhousing<- txhousing %>% mutate(logsales= log(txhousing$sales))    
txhousing %>% group_by(txhousing$month) %>% summarise(mean(logsales), median(logsales)) %>% arrange(mean)

I expect to get a table with the mean and median of the logsales per month, but what i get is only NA in the column from the mean en the median and the arrange gives the following error:

Error: cannot arrange column of class 'function' at position 1`

1

1 Answers

0
votes

There are NA values in the columns, so you need to tell mean and median to ignore them. And also name the columns in summarise to use arrange on column named as mean.

txhousing %>% 
  group_by(txhousing$month) %>% 
  summarise(mean = mean(logsales, na.rm = T), 
            med= median(logsales, na.rm = T)) %>% 
  arrange(mean) %>%
  rename(month = `txhousing$month`)

This creates following tibble

# A tibble: 12 x 3
   month  mean   med
   <int> <dbl> <dbl>
 1     1  4.95  4.74
 2     2  5.13  4.93
 3    11  5.19  4.96
 4    12  5.24  5.02
 5    10  5.29  5.08
 6     9  5.32  5.09
 7     3  5.38  5.15
 8     4  5.42  5.21
 9     5  5.52  5.29
10     7  5.53  5.30
11     8  5.53  5.33
12     6  5.56  5.34