Syd Amerikaner
Syd Amerikaner

Reputation: 105

dplyr-like row manipulation

let's say I have a data.frame

A B C
x q 4
x p 2
y q 0
y p 8

I want to select all rows with B==q and calculate the corresponding mean of C and add it as an additional row with let's say A==z, B==o and for C the just caluclated mean.

This is basically what dplyr does with columns but applied to rows. My question is thus: Is there any way to apply dplyr operations instead of columns on rows? Even better of course would be dplyr-like package for row-manipulation.

(My actual data.frame consists of course of a lot more rows and columns)

Best regards

Upvotes: 0

Views: 170

Answers (2)

CSV
CSV

Reputation: 849

I would say use group_by and summarise function in dplyr

df %>% group_by (B) %>% group_by(A) %>% summaries(mean(C))

Upvotes: 0

nassimhddd
nassimhddd

Reputation: 8510

The way to do this using dplyr is as follows:

df <-  read.table(header = TRUE, 
text = "A B C
  x q 4
  x p 2
  y q 0
  y p 8")

library(dplyr)

df %>% bind_rows(df %>% 
                filter(B=="q") %>% 
                summarize(C = mean(C)) %>%
                mutate(A = "z", B = "o")
              )

Upvotes: 1

Related Questions