[R] Dataframe: Average cells of two rows and replace them with one row

Rui Barradas ruipbarradas at sapo.pt
Tue May 27 22:24:53 CEST 2014


Hello,

Try the following.

dat <- read.table(text = "
Name C1 C2 C3
   1  A  3  3  5
   2  B  2  7  4
   3  C  4  3  3
   4  C  4  4  6
   5  D  5  5  3
", header = TRUE)

str(dat)

aggregate(dat[, -1], list(dat$Name), mean)


Hope this helps,

Rui Barradas

Em 27-05-2014 21:06, Verena Weinbir escreveu:
> Hello,
>
> I have a big dataframe, and want to average two specific cells of two
> specific rows and then replace those two rows with one row which contains
> the averaged cells. Example (row 3 and 4: Cells2 and Cells3 averaged and
> replaced)
>
>      NameC1 C2 C3
>    1  A  3  3  5
>    2  B  2  7  4
>    3  C  4  3  3
>    4  C  4  4  6
>    5  D  5  5  3
>
>
>
>      NameC1 C2  C3
>    1  A  3  3   5
>    2  B  2  7   4
>    3  C  4  3.5 4.5  4  D  5  5   3
>
>
> Many thanks in advance!
>
> Best,
>
> Verena
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list