-2

I try to get out of my data a table and I don't know how to do this. This is my data. The header is in German. Because of this is my r-code in german too. sorry for that but I hope someone can help me with the subject .

data frame

I would like to get this output as a pivot table in r. I tryed to translate in english:

pivot in excel

In r i tried different ways to get to a solution (library(pivottabler):

This on worked a bit, but I couldn't calculate the median. counting the rows

version with testing median calculation

Here I don' get the value into the table table without values

I also tried this. But this is not the output I am looking for. group_by and summarise

Thanks for your help. mg

Mgripp
  • 1
  • 1
  • 1
    Hi to Germany, Welcome to stackoverflow. Please go through this . – TarJae Jan 22 '22 at 15:12
  • What is `3'057.63` ? The same as 3057.63 = Three thousand fifty seven comma sixty three? Big-mark? or grade? TIME? – TarJae Jan 22 '22 at 15:18
  • 1
    it is earnings not time like euro – Mgripp Jan 22 '22 at 15:38
  • It's hard to follow what you're doing and what the problem is trying to read & compare data between pictures, and without knowing what your headers are. That's why we ask for a [mcve] – camille Jan 22 '22 at 16:18
  • Total mean of what,? and which variables want you to calculate? ` K_CHF_GV DB3 DB3_rel L_294_OPmin` as in your png or all? – TarJae Jan 22 '22 at 16:22

2 Answers2

0

It would be easier to give you a more applicable answer if you gave us a more reproducible example.

However, what I think you want is easily accomplished with functions from the dplyr package. This answer uses the built-in iris dataset. It groups by the categorical variable Species and calculates the median by Species for all numeric columns in the data frame.

library(dplyr)
iris %>%
  group_by(Species) %>%
  summarise(across(where(is.numeric), median))
# A tibble: 3 x 5
  Species    Sepal.Length Sepal.Width Petal.Length Petal.Width
  <fct>             <dbl>       <dbl>        <dbl>       <dbl>
1 setosa              5           3.4         1.5          0.2
2 versicolor          5.9         2.8         4.35         1.3
3 virginica           6.5         3           5.55         2  

To calculate the mean over all numeric columns, use mean where the above code has median.

Ben Norris
  • 5,639
  • 2
  • 6
  • 15
0

I found a way to get the pivot right in R. I used the library("pivottabler") with the data.frame "bhmtrains". This worked now. Thanks for your ideas.

result of the pivottabler

library(pivottabler)
qhpvt(bhmtrains, c("=","TOC"), "TrainCategory", 
  c("Mean Speed"="mean(SchedSpeedMPH, na.rm=TRUE)", "Std Dev Speed"="sd(SchedSpeedMPH, na.rm=TRUE)"), 
  formats=list("%.0f", "%.1f"), totals=list("", "TrainCategory"="All Categories"))
Mgripp
  • 1
  • 1