2

i am new to R and a bit overwhelmed by an assignment. i am asked to create a new dataframe out of an existing one ( the diamonds data that come preinstalled with ggplot2). The dataframe should look as follows:

mean_price median_price min_price max_price n

All sorted by clarity where n is the number of entries in that clarity category.

  • I know i can access the clarity with diamonds$clarity

  • and i know i can get mean, median etc... with mean(diamonds$price) etc..

But how do i sort it according to clarity and put it into a new dataframe ?

Frank
  • 66,179
  • 8
  • 96
  • 180
hmmmbob
  • 1,167
  • 5
  • 19
  • 33
  • 1
    I can't find the diamonds dataset. `data(diamonds)` ##Warning message: In data(diamonds) : data set ‘diamonds’ not found` Perhaps`diamonds[order(diamonds$clarity),]` or using `dplyr`, `arrange(diamonds, clarity)` – akrun May 24 '15 at 11:04
  • You should have mentioned that the diamonds is from library(ggplot2) – akrun May 24 '15 at 11:12
  • Just shows what a noobie i am ... i am sorry for forgetting that – hmmmbob May 24 '15 at 11:20
  • the dataframe needs to include the mean etc.. of all the data.. but sorted by clarity.. So mean of the price when clarity is a, b , c etc... – hmmmbob May 24 '15 at 11:24
  • Can you update your post with the expected result as using `group_by` it is already sorting by 'clarity' – akrun May 24 '15 at 11:32
  • 1
    I suggest you read the following , it wasn't easy to understand your question http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Nader Hisham May 24 '15 at 11:36

1 Answers1

3
library(ggplot2)

  diamonds %>%
      group_by(clarity) %>%

      summarise(mean_price = mean(price) ,  min_price =min(price) ,max_price = max(price) ,
      median_price = median(as.numeric(price)),  count = n()) %>%

      arrange(clarity)

for arranging in descending order use arrange(desc(clarity)) instead of arrange(clarity)

Nader Hisham
  • 5,214
  • 4
  • 19
  • 35
  • I don't think there is any difference in using `arrange` here, Just check the output with and without it. The `group_by` already sorts it by clarity – akrun May 24 '15 at 11:29
  • yeah you're right , any way I don't know whether he is going to arrange it in ascending or descending order , he will need the `arrange` if he wants to arrange it in descending order – Nader Hisham May 24 '15 at 11:33
  • Hmm i tried to copy it and assign it to a variable... just nothing happened :( what am i missing :( – hmmmbob May 24 '15 at 16:15