Diin
Diin

Reputation: 585

Maximum and minimum value of a dataset in r

Hi i am new to r and I have to solve this question below

Compare the maximum and minimum values within each group (factor level) to their respective group means. What is the largest absolute difference between one of your values and its group mean? What are the chances of obtaining such a value, assuming the data are normally distributed and centered around the respective group mean with a standard deviation of 1?

The dataset and frame was generated by

  fact<-rep(c("E","F","G","H"),each=12)
  variable2=rnorm(48,10)*(rep(rpois(4,.2),each=12)/8+1)
  ds<-data.frame(fact,variable2)

Any help will be appreciated

This is what I have tried

library(“plyr”)
ddply(ds,~fact,summarise,maximum=max(variable2),min=min(variable2),mean=mean(variable2))

Upvotes: 0

Views: 2669

Answers (1)

Simon O&#39;Hanlon
Simon O&#39;Hanlon

Reputation: 59980

You were nearly there. The dnorm function will help you here

res <- ddply(ds, ~fact , 
                summarise , 
                maxi = max(variable2) - mean(variable2),
                mini = min(variable2) - mean(variable2) )

res$probmax <- dnorm( res$maxi )
res$probmin <- dnorm( res$mini )
#  fact      maxi      mini    probmax      probmin
#1    E 1.7736537 -1.622157 0.08275571 0.1070311818
#2    F 1.7733593 -2.269254 0.08279894 0.0303883803
#3    G 2.6621257 -3.708242 0.01153470 0.0004120085
#4    H 0.8461922 -1.749625 0.27888407 0.0863339664

Upvotes: 1

Related Questions