Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R, what command do I use to generate a dataset consisting of the means of all column vectors in a dataset?

Some background: First, I wanted to generate multiple sets of samples (each of sample size n) from a uniform (0,1) distribution in [R]. I know that the command for generating from a uniform distribution is runif(n=x) for some sample size x, e.g. if I wanted sample size 20 the command would be

runif(n=20)

Next, I used the command

replicate( 100, runif(n=20))

This generated a double matrix of values which I could then convert into a dataset with 100 columns and 20 rows.

Is it possible for me to generate a dataset consisting of the sample means of all the column vectors (the sample means of the 100 sets taken from the uniform distribution)?

Thank you for your help.

like image 396
user1357062 Avatar asked Apr 25 '12 19:04

user1357062


People also ask

How do I create a vector dataset in R?

To create a vector of data frame values by rows we can use c function after transposing the data frame with t. For example, if we have a data frame df that contains many columns then the df values can be transformed into a vector by using c(t(df)), this will print the values of the data frame row by row.

How do I add a column to a dataset in R?

How to add columns to a data frame in R?, To add one or more columns to a data frame in R, use the mutate() function from the dplyr package. With the following data frame, the following examples demonstrate how to use this syntax in practice.


1 Answers

You can use colMeans.

data <- replicate(100, runif(n=20))
means <- colMeans(data)
like image 102
nico Avatar answered Oct 20 '22 11:10

nico