Will Knight Will Knight - 1 year ago 69
R Question

Use first part of value as header in r

I have a data frame generated by


Original data frame

However, each cell has a prefix of like max, min, mean, etc... And I would like to remove that prefix from each row and put it at the header. Is there an easy way to do this in r to get the dataframe to look like this:

Desired data frame

Also, as far as variables 3 & 18 which are factors. Those i'm less concerned about.

Answer Source

We can loop through the columns of the dataset, get the summary and then rbind the output

do.call(rbind, lapply(raw_data, summary))

Using a reproducible example

do.call(rbind, lapply(iris[1:4], summary))
#              Min. 1st Qu. Median  Mean 3rd Qu. Max.
#Sepal.Length  4.3     5.1   5.80 5.843     6.4  7.9
#Sepal.Width   2.0     2.8   3.00 3.057     3.3  4.4
#Petal.Length  1.0     1.6   4.35 3.758     5.1  6.9
#Petal.Width   0.1     0.3   1.30 1.199     1.8  2.5