I am having trouble finding the mean of a specific part of my dataset. Here is a sample of it: plot lai leaf 1 104 82 1 2 104 167 2 3 104 248 3 4 104 343 4 5 104 377 5 6 105 64 1 7 105 139 2 8 105 211 3 9 105 296 4 10 105 348 5 11 106 94 1 12 106 167 2 13 106 243 3 14 106 281 4 15 106 332 5 16 108 83 1 17 108 382 2 18 108 320 3 19 108 146 4 20 108 129 5 I have many different plot numbers, none of which follow any kind of specific numeric sequence (even though I have sorted them from smallest to largest). I need to take the average (mean) of the LAI for each plot, and was wondering if there was a way to specify the code to do this. For example: I need the average of all the LAI measurements for each leaf of plot 104, 105, etc. Any help would be appreciated. Thanks! Get Outlook for iOS<https://aka.ms/o0ukef> [[alternative HTML version deleted]]
Hi, try library("dplyr") plot <- c(104, 104 ,104 ,104 ,104 ,105 ,105 ,105 ,105 ,105,106, 106,106, 106, 106,108, 108,108,108,108) lai <- c(82, 167, 248, 343, 377, 64, 139, 211, 296, 348, 94, 167,243,281,332,83, 382,320,146,129) leaf <- c(1,2, 3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5) df <- cbind(plot, lai, leaf) %>% data.frame() df %>% dplyr::group_by(plot) %>% dplyr::summarise(mean = mean(lai, na.rm = T)) Best, Frederick -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Isaac Barnhart Sent: donderdag 14 februari 2019 15:32 To: r-help at r-project.org Subject: [R] Finding the Mean of a Specific Set of Columns I am having trouble finding the mean of a specific part of my dataset. Here is a sample of it: plot lai leaf 1 104 82 1 2 104 167 2 3 104 248 3 4 104 343 4 5 104 377 5 6 105 64 1 7 105 139 2 8 105 211 3 9 105 296 4 10 105 348 5 11 106 94 1 12 106 167 2 13 106 243 3 14 106 281 4 15 106 332 5 16 108 83 1 17 108 382 2 18 108 320 3 19 108 146 4 20 108 129 5 I have many different plot numbers, none of which follow any kind of specific numeric sequence (even though I have sorted them from smallest to largest). I need to take the average (mean) of the LAI for each plot, and was wondering if there was a way to specify the code to do this. For example: I need the average of all the LAI measurements for each leaf of plot 104, 105, etc. Any help would be appreciated. Thanks! Get Outlook for iOS<https://aka.ms/o0ukef> [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Hi Isaac, I am sure you will get lots of answers to this. Here is one using the dplyr package. Assuming that your data frame is called 'a', then library(dplyr) b <- dplyr::group_by(a,plot) %>% dplyr::summarise( mean(lai) ) b # A tibble: 4 x 2 plot `mean(lai)` <int> <dbl> 1 104 243. 2 105 212. 3 106 223. 4 108 212 HTH, Eric On Thu, Feb 14, 2019 at 6:20 PM Isaac Barnhart <ihb at ksu.edu> wrote:> I am having trouble finding the mean of a specific part of my dataset. > Here is a sample of it: > > plot lai leaf > 1 104 82 1 > 2 104 167 2 > 3 104 248 3 > 4 104 343 4 > 5 104 377 5 > 6 105 64 1 > 7 105 139 2 > 8 105 211 3 > 9 105 296 4 > 10 105 348 5 > 11 106 94 1 > 12 106 167 2 > 13 106 243 3 > 14 106 281 4 > 15 106 332 5 > 16 108 83 1 > 17 108 382 2 > 18 108 320 3 > 19 108 146 4 > 20 108 129 5 > > I have many different plot numbers, none of which follow any kind of > specific numeric sequence (even though I have sorted them from smallest to > largest). I need to take the average (mean) of the LAI for each plot, and > was wondering if there was a way to specify the code to do this. For > example: I need the average of all the LAI measurements for each leaf of > plot 104, 105, etc. Any help would be appreciated. Thanks! > > Get Outlook for iOS<https://aka.ms/o0ukef> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
Hi, You might try your hand at the tidyverse collection of tools which are veddy nice for this kind of wrangling. https://www.tidyverse.org/ Does this do the trick? ## START library(readr) library(dplyr) txt <- "row plot lai leaf 1 104 82 1 2 104 167 2 3 104 248 3 4 104 343 4 5 104 377 5 6 105 64 1 7 105 139 2 8 105 211 3 9 105 296 4 10 105 348 5 11 106 94 1 12 106 167 2 13 106 243 3 14 106 281 4 15 106 332 5 16 108 83 1 17 108 382 2 18 108 320 3 19 108 146 4 20 108 129 5" x <- readr::read_delim(txt, delim = " ") %>% dplyr::group_by(plot) %>% dplyr::summarize(mean_lai = mean(lai)) x # A tibble: 4 x 2 # plot mean_lai # <dbl> <dbl> # 1 104 243. # 2 105 212. # 3 106 223. # 4 108 212 ## END Cheers, Ben> [[alternative HTML version deleted]]P.S. Don't forget that HTML formatted emails get stripped of formatting on this list - so be sure to change your email client to send plain text.> On Feb 14, 2019, at 9:31 AM, Isaac Barnhart <ihb at ksu.edu> wrote: > > I am having trouble finding the mean of a specific part of my dataset. Here is a sample of it: > > plot lai leaf > 1 104 82 1 > 2 104 167 2 > 3 104 248 3 > 4 104 343 4 > 5 104 377 5 > 6 105 64 1 > 7 105 139 2 > 8 105 211 3 > 9 105 296 4 > 10 105 348 5 > 11 106 94 1 > 12 106 167 2 > 13 106 243 3 > 14 106 281 4 > 15 106 332 5 > 16 108 83 1 > 17 108 382 2 > 18 108 320 3 > 19 108 146 4 > 20 108 129 5 > > I have many different plot numbers, none of which follow any kind of specific numeric sequence (even though I have sorted them from smallest to largest). I need to take the average (mean) of the LAI for each plot, and was wondering if there was a way to specify the code to do this. For example: I need the average of all the LAI measurements for each leaf of plot 104, 105, etc. Any help would be appreciated. Thanks! > > Get Outlook for iOS<https://aka.ms/o0ukef> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.Ben Tupper Bigelow Laboratory for Ocean Sciences 60 Bigelow Drive, P.O. Box 380 East Boothbay, Maine 04544 http://www.bigelow.org Ecological Forecasting: https://eco.bigelow.org/
On Feb 14, 2019, at 9:31 AM, Isaac Barnhart <ihb at ksu.edu> wrote:> > I am having trouble finding the mean of a specific part of my dataset. Here is a sample of it: > > plot lai leaf > 1 104 82 1 > 2 104 167 2 > 3 104 248 3 > 4 104 343 4 > 5 104 377 5 > 6 105 64 1 > 7 105 139 2 > 8 105 211 3 > 9 105 296 4 > 10 105 348 5 > 11 106 94 1 > 12 106 167 2 > 13 106 243 3 > 14 106 281 4 > 15 106 332 5 > 16 108 83 1 > 17 108 382 2 > 18 108 320 3 > 19 108 146 4 > 20 108 129 5 > > I have many different plot numbers, none of which follow any kind of specific numeric sequence (even though I have sorted them from smallest to largest). I need to take the average (mean) of the LAI for each plot, and was wondering if there was a way to specify the code to do this. For example: I need the average of all the LAI measurements for each leaf of plot 104, 105, etc. Any help would be appreciated. Thanks!Hi, This is easy using base R functions. See ?aggregate, ?by and ?tapply for a starting place. For example:> aggregate(lai ~ plot, data = DF, FUN = mean)plot lai 1 104 243.4 2 105 211.6 3 106 223.4 4 108 212.0 Regards, Marc Schwartz
Thanks for the help! Isaac ________________________________ From: Marc Schwartz <marc_schwartz at me.com> Sent: Thursday, February 14, 2019 11:05:23 AM To: Isaac Barnhart Cc: R-help Subject: Re: [R] Finding the Mean of a Specific Set of Columns On Feb 14, 2019, at 9:31 AM, Isaac Barnhart <ihb at ksu.edu> wrote:> > I am having trouble finding the mean of a specific part of my dataset. Here is a sample of it: > > plot lai leaf > 1 104 82 1 > 2 104 167 2 > 3 104 248 3 > 4 104 343 4 > 5 104 377 5 > 6 105 64 1 > 7 105 139 2 > 8 105 211 3 > 9 105 296 4 > 10 105 348 5 > 11 106 94 1 > 12 106 167 2 > 13 106 243 3 > 14 106 281 4 > 15 106 332 5 > 16 108 83 1 > 17 108 382 2 > 18 108 320 3 > 19 108 146 4 > 20 108 129 5 > > I have many different plot numbers, none of which follow any kind of specific numeric sequence (even though I have sorted them from smallest to largest). I need to take the average (mean) of the LAI for each plot, and was wondering if there was a way to specify the code to do this. For example: I need the average of all the LAI measurements for each leaf of plot 104, 105, etc. Any help would be appreciated. Thanks!Hi, This is easy using base R functions. See ?aggregate, ?by and ?tapply for a starting place. For example:> aggregate(lai ~ plot, data = DF, FUN = mean)plot lai 1 104 243.4 2 105 211.6 3 106 223.4 4 108 212.0 Regards, Marc Schwartz [[alternative HTML version deleted]]