Create a matrix out of functions

R_ocky

I couldn't a find a solution so far ... I have a data frame X of 7 colums. I want to get statistical data from colums 6 and 7 which are collected in a matrix. I would like to use one line of code for that issue. I need a function wich takes colum 6 and 7 of data frame X and calculates mean, var, sd, ...

The code I have used is this one:

matrix(c(mean(X$6), mean(X$6, trim = 0.05), var(X$6), sd(X$6),
         mean(X$7), mean(X$7, trim = 0.05), var(X$7), sd(X$7)),
         nr = 4, dimnames = list(c("mean", "meanTrim", "var", "sd"), 
                                 c("WorkTime", "TransTime")))

It works but it seems to be quite awkward. Do you know a shorter solution?

user2474226

You could do something like:

Y <- sapply(X[, 6:7], function(i) c(mean(i), mean(i, trim = 0.05), var(i), sd(i)))
dimnames(Y) <- list(c("mean", "meanTrim", "var", "sd"), c("WorkTime", "TransTime"))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Tensorflow stack vectors out of loop to create a matrix

Create matrix with same in and out degree for all nodes

How to create a Matrix out of a list of timeseries

How to create Eigen matrix out of 2 vectors

R how to use loop functions to create a matrix

for-loop or create functions to create matrix (or dataframe) and rename columns

How to create a 2D-matrix out of my data for surf()?

OpenCV: cv::Mat fails to create a matrix out of seemingly valid data

Matlab - Create a bigger matrix out of a smaller one with blank spaces

How can create a sparse matrix out of outer function in R?

How to create a matrix out of different matrices with different number of rows in matlab?

Create a 10^4 x 3 matrix the columns correspond to various functions

Create diagonal matrix without using MATLAB built-in functions

Create a matrix out of remaining data from a random row selection of a matrix, and use data to calculate RMSE in R

Create matrix out of two lists while populating body of matrix using custom method in Python

Return matrix out of index

Use dask for an out of core conversion of iterable.product into a numpy/dask array (create a matrix of every permutation with repetition)

Create custom matrix from values coming out of vector calculations using only the highest number

matlab matrix functions in java

matrix of special (besselj) functions

Substitute functions in sympy matrix

dafny matrix expressions and functions

Create a boolean matrix from a matrix

Create a matrix with another matrix in R

Create a matrix from a sub matrix?

Rails - Create nil value when performing grouped functions on filtered out values

Create C++ dll with functions whose arguments can be called as out parameter in c#

Create a functions, which creates a new array out of an existing one, which has less or equal amount of elements and no repeats

How to build a matrix out of an matrix by column index?