Verena Weinbir
2014-May-27 20:06 UTC
[R] Dataframe: Average cells of two rows and replace them with one row
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]]
Greg Snow
2014-May-27 20:13 UTC
[R] Dataframe: Average cells of two rows and replace them with one row
Look at the aggregate function. As long as you have a column like Name that indicates which rows should be averaged together it will work (technically it will average the other rows as well, but since the average of 1 number is that number you will not see a difference). On Tue, May 27, 2014 at 2:06 PM, Verena Weinbir <vweinbir at gmail.com> wrote:> 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.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com
Rui Barradas
2014-May-27 20:24 UTC
[R] Dataframe: Average cells of two rows and replace them with one row
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. >
arun
2014-May-28 01:48 UTC
[R] Dataframe: Average cells of two rows and replace them with one row
Hi, You can also try: 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",sep="",header=TRUE,stringsAsFactors=FALSE) ?library(plyr) ?ddply(dat,.(Name),numcolwise(mean,na.rm=TRUE)) A.K. On Tuesday, May 27, 2014 4:08 PM, Verena Weinbir <vweinbir at gmail.com> wrote: 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.