Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

I am currently working with the diamonds dataset and from that, I want to return the row corresponding to the smallest, mean, and largest prices and put everything in a 10 by 4 matrix. 

library(ggplot2)

data(diamonds)

min(diamonds$price) 

mean(diamonds$price)

max(diamonds$price) # this one gives me the wrong val!

M<-matrix(1:cols, nrow = 1, ncol = cols)

colnames(M)<-c("carat","cut" , "color" , "clarity", "depth" , "table" , "price" ,  "x" , "y" ,"z")

# Here I need to add the rows corresponding to the min, mean, max to this matrix.

1 Answer

0 votes
by (107k points)

Let me tell you that sub-setting and matrices are two different things. If you check the summary(diamonds), it will give you some nice summary statistics for each column.

If you want to execute some specific functions to columns using dplyr, you can do :

library(dplyr)

diamonds %>%

  summarise(across(where(is.numeric),list(min = min, max = max, mean = mean))) %>%

  tidyr::pivot_longer(cols = everything(), 

                      names_to = c('col', '.value'), 

                      names_sep = '_') 

# A tibble: 7 x 4

#  col     min      max     mean

#  <chr> <dbl>    <dbl>    <dbl>

#1 carat   0.2     5.01    0.798

#2 depth  43      79      61.7  

#3 table  43      95      57.5  

#4 price 326   18823    3933.   

#5 x       0      10.7     5.73 

#6 y       0      58.9     5.73 

#7 z       0      31.8     3.54 

Note that I applied these functions only to numeric columns as the cut, color, clarity are factor columns.

If you are a beginner and want to know what is R programming then do checkout the blog.

Related questions

0 votes
1 answer
0 votes
0 answers
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...