-1

I want to make a table of mean values, standard deviation, max and min for a data frame, until know I've achieved this table with this code.

dt<-data.frame(nuevabase$años_de_educacion,group=factor(nuevabase$año))

abs<-ddply(dt,~ group,summarise,mean=mean(nuevabase$años_de_educacion,na.rm=TRUE)
           ,sd=sd(nuevabase$años_de_educacion,na.rm=TRUE)
           ,max=max(nuevabase$años_de_educacion,na.rm=TRUE)
           ,min=min(nuevabase$años_de_educacion,na.rm=TRUE))

but the problem is that the table gives me the mean value of the whole years, and I only want a mean value for each year, how I can change this? enter image description here

s__
  • 9,270
  • 3
  • 27
  • 45

2 Answers2

2

What about this: due you have not posted your data, here an example with the famous diamonds dataset. Cut is like your group, depth like años_de_educacion:

 library(dplyr)
 diamonds %>% group_by(cut) %>% summarise(
    avg = mean(depth),
    stdev = sd(depth),
    minval = min(depth),
    maxval = max(depth))

So in your case:

dt<-data.frame(años_de_educacion =nuevabase$años_de_educacion,
               group=factor(nuevabase$año))

 abs<- dt %>% group_by(group) %>% summarise(
    avg = mean(años_de_educacion),
    stdev = sd(años_de_educacion),
    minval = min(años_de_educacion),
    maxval = max(años_de_educacion))
s__
  • 9,270
  • 3
  • 27
  • 45
1

In base R, you can do this with aggregate. I will use the built-in dataset iris to give an example.

agg <- aggregate(Sepal.Length ~ Species, iris, function(x){
  c(mean = mean(x), sd = sd(x), min = min(x), max = max(x))
})

cbind(agg[1], agg[[2]])
#     Species  mean        sd min max
#1     setosa 5.006 0.3524897 4.3 5.8
#2 versicolor 5.936 0.5161711 4.9 7.0
#3  virginica 6.588 0.6358796 4.9 7.9

Following the (good) example of user @s_t, the code for your dataset would be

agg <- aggregate(años_de_educacion ~ año, nuevabase, function(x){
  c(mean = mean(x), sd = sd(x), min = min(x), max = max(x))
})

And then the same cbind instruction.
Note that you don't need an explicit coercion of año to class factor, R is clever enough to do it on its own.

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66