Create a table for N, Min / Max, SD, Mean and Median in R

I am very new to R , so please bear with me on this basic subject. I have a DATA dataset that I created using the data.table package. I created 200 random numbers between 0 and 1, and then did it 10,000 times, finally creating a data table with descriptive statistics for each iteration. My code for it looked like this:

rndm<-runif(200, min=0, max=1) reps <- data.table(x=runif(200*10000),iter=rep(1:200,each=10000)) DATA <- reps[,list(mean=mean(rndm),median=median(rndm),sd=sd(rndm),min=min(rndm), max=max(rndm)),by=iter] 

The data looks something like this:

  Mean Median SD Min Max 1 0.521 0.499 0.287 0.010 0.998 2 0.511 0.502 0.290 0.009 0.996 . ... ... 

and etc.

What I want to do is create a table that finds the average N, average, average, standard deviation, minimum and maximum accumulated sample (not for each column, as indicated above). I need some output to look something like this:

  N Mean Median SD Min Max 10000 .502 .499 .280 .002 .999 

How can i do this?

+4
source share
2 answers

You can also define a function. This approach allows you to make the same table for another variable.

 summaryfun <- function(x)list(N=length(x),Mean=mean(x),Median=median(x),SD=sd(x),Min=min(x),Max=max(x)) DATA[,summaryfun(mean)] 
+6
source

Currently, you are calculating the functions in the list separately for each item other than iter . But if you want to get general statistics, just delete the by clause, and your functions will be executed once, across the entire data set. Then add an element to give N - the use of the .N variable provided by data.table.

 DATA <- reps[, list(N=.N, mean=mean(rndm), median=median(rndm), sd=sd(rndm), min=min(rndm), max=max(rndm))] 
+4
source

Source: https://habr.com/ru/post/986790/


All Articles