Is there an R function for finding the avg index of change (x[n]/x[1])^(1/(n-1))?How to make a great R reproducible exampleFunction to Calculate Median in SQL ServerIs there a built-in function for finding the mode?Grouping functions (tapply, by, aggregate) and the *apply familyIs there an R function for finding the index of an element in a vector?Changing column names of a data frameFind running median from a stream of integersFunction to clear the console in R and RStudioHow can I view the source code for a function?Finding median of list in PythonRolling / moving avg by group

Are there examples in Tanach of 3 or more parties having an ongoing conversation?

Why aren't rainbows blurred-out into nothing after they are produced?

Crippling fear of hellfire &, damnation, please help?

Why does Japan use the same type of AC power outlet as the US?

"Table of Astronomy's" depiction of the solar system models

Why aren't rockets built with truss structures inside their fuel & oxidizer tanks to increase structural strength?

What can Amex do if I cancel their card after using the sign up bonus miles?

Does an Irish VISA WARNING count as "refused entry at the border of any country other than the UK?"

How do some PhD students get 10+ papers? Is that what I need for landing good faculty position?

A trip to the library

Dogfights in outer space

How should I write this passage to make it the most readable?

How would you translate this? バタコチーズライス

Will using a resistor in series with a LED to control its voltage increase the total energy expenditure?

Are there any cons in using rounded corners for bar graphs?

Why does cat'ing a file via ssh result in control characters?

Did DOS zero out the BSS area when it loaded a program?

NRPE script for monitoring load average

Cases with long math equation

Letting unbanned users comment

What is the たんだ in と思ってたんだ for the sentence in question?

What is the safest way to leave my MacBook on 24/7 with macOS Mojave?

Why is there a large performance impact when looping over an array over 240 elements?

"Mouth-breathing" as slang for stupidity



Is there an R function for finding the avg index of change (x[n]/x[1])^(1/(n-1))?


How to make a great R reproducible exampleFunction to Calculate Median in SQL ServerIs there a built-in function for finding the mode?Grouping functions (tapply, by, aggregate) and the *apply familyIs there an R function for finding the index of an element in a vector?Changing column names of a data frameFind running median from a stream of integersFunction to clear the console in R and RStudioHow can I view the source code for a function?Finding median of list in PythonRolling / moving avg by group






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















this is my first post on this website so - sorry for any mistakes.



Given a data frame



xyz <- data.frame(GEO=c("Belgium", "Belgium", "Belgium", "Germany", "Germany", "Germany", "Italy", "Italy", "Italy"),
year=c(2008, 2009, 2010, 2008, 2009, 2010, 2008, 2009, 2010),
median=c(10.3, 45.2, 34.2, 67.8, 68.7, 69.9, 53.2, 43.2, 33.3),
imigration=c(10.2, 45.2, 47.4, 33.3, 44.4, 55.5, 21.5, 76.5, 78.4))


All I want to do is to count avg index of change for median and imigration.



index=(x[n]/x[1])^(1/(n-1)) 


I tried a lot of solutions but I couldn't find the good one.
This is my code - maybe you will use this to help me.



First:



groupColumns = c("GEO","year")
dataColumns = c("median", "imigration")
index_median = ddply(mediana2, groupColumns, function(x) x[c((nrow(x)/x[1])^(1/nrow(x)-1)), ])
head(index_median)


Second:



xyz_index <- aggregate(xyz$median, xyz$imigration by=list(Category=xyz$"GEO/TIME"), FUN=median$index_median[nrow(median)]/median[1])^(1/(nrow(median)-1))


I want to get avg index of change for median and imigration (index_median, index_imigration) for every country (Belgium, Germany, Italy).
Something like that (numbers are fake):



 GEO index_median index_imigration
1 Germany 2.3 1.3
2 Italy 1.6 1.6
3 Belgium 1.2 2.0


Thank you very much!










share|improve this question





















  • 1





    Can you edit your question and include your expected output: i.e. what should your result(s) look like?

    – Phil
    Mar 27 at 11:06











  • Thank you for your advice, done!

    – wloczykijek
    Mar 27 at 11:10











  • @wloczykijek Welcome to SO I think you should read this. Meanwhile, do you want something like library(dplyr);xyz %>% group_by(GEO) %>% mutate(Ind=((median/first(median))^(1/(row_number()-1))))

    – A. Suliman
    Mar 27 at 11:15






  • 1





    @A.Suliman I edited my post - you can see what exactly I want to get

    – wloczykijek
    Mar 27 at 11:29






  • 1





    Now is better, one last question what n stand for in index=(x[n]/x[1])^(1/(n-1)), I think it's the last obs e.g. for Belgium's median it's 34.2

    – A. Suliman
    Mar 27 at 11:32


















2















this is my first post on this website so - sorry for any mistakes.



Given a data frame



xyz <- data.frame(GEO=c("Belgium", "Belgium", "Belgium", "Germany", "Germany", "Germany", "Italy", "Italy", "Italy"),
year=c(2008, 2009, 2010, 2008, 2009, 2010, 2008, 2009, 2010),
median=c(10.3, 45.2, 34.2, 67.8, 68.7, 69.9, 53.2, 43.2, 33.3),
imigration=c(10.2, 45.2, 47.4, 33.3, 44.4, 55.5, 21.5, 76.5, 78.4))


All I want to do is to count avg index of change for median and imigration.



index=(x[n]/x[1])^(1/(n-1)) 


I tried a lot of solutions but I couldn't find the good one.
This is my code - maybe you will use this to help me.



First:



groupColumns = c("GEO","year")
dataColumns = c("median", "imigration")
index_median = ddply(mediana2, groupColumns, function(x) x[c((nrow(x)/x[1])^(1/nrow(x)-1)), ])
head(index_median)


Second:



xyz_index <- aggregate(xyz$median, xyz$imigration by=list(Category=xyz$"GEO/TIME"), FUN=median$index_median[nrow(median)]/median[1])^(1/(nrow(median)-1))


I want to get avg index of change for median and imigration (index_median, index_imigration) for every country (Belgium, Germany, Italy).
Something like that (numbers are fake):



 GEO index_median index_imigration
1 Germany 2.3 1.3
2 Italy 1.6 1.6
3 Belgium 1.2 2.0


Thank you very much!










share|improve this question





















  • 1





    Can you edit your question and include your expected output: i.e. what should your result(s) look like?

    – Phil
    Mar 27 at 11:06











  • Thank you for your advice, done!

    – wloczykijek
    Mar 27 at 11:10











  • @wloczykijek Welcome to SO I think you should read this. Meanwhile, do you want something like library(dplyr);xyz %>% group_by(GEO) %>% mutate(Ind=((median/first(median))^(1/(row_number()-1))))

    – A. Suliman
    Mar 27 at 11:15






  • 1





    @A.Suliman I edited my post - you can see what exactly I want to get

    – wloczykijek
    Mar 27 at 11:29






  • 1





    Now is better, one last question what n stand for in index=(x[n]/x[1])^(1/(n-1)), I think it's the last obs e.g. for Belgium's median it's 34.2

    – A. Suliman
    Mar 27 at 11:32














2












2








2








this is my first post on this website so - sorry for any mistakes.



Given a data frame



xyz <- data.frame(GEO=c("Belgium", "Belgium", "Belgium", "Germany", "Germany", "Germany", "Italy", "Italy", "Italy"),
year=c(2008, 2009, 2010, 2008, 2009, 2010, 2008, 2009, 2010),
median=c(10.3, 45.2, 34.2, 67.8, 68.7, 69.9, 53.2, 43.2, 33.3),
imigration=c(10.2, 45.2, 47.4, 33.3, 44.4, 55.5, 21.5, 76.5, 78.4))


All I want to do is to count avg index of change for median and imigration.



index=(x[n]/x[1])^(1/(n-1)) 


I tried a lot of solutions but I couldn't find the good one.
This is my code - maybe you will use this to help me.



First:



groupColumns = c("GEO","year")
dataColumns = c("median", "imigration")
index_median = ddply(mediana2, groupColumns, function(x) x[c((nrow(x)/x[1])^(1/nrow(x)-1)), ])
head(index_median)


Second:



xyz_index <- aggregate(xyz$median, xyz$imigration by=list(Category=xyz$"GEO/TIME"), FUN=median$index_median[nrow(median)]/median[1])^(1/(nrow(median)-1))


I want to get avg index of change for median and imigration (index_median, index_imigration) for every country (Belgium, Germany, Italy).
Something like that (numbers are fake):



 GEO index_median index_imigration
1 Germany 2.3 1.3
2 Italy 1.6 1.6
3 Belgium 1.2 2.0


Thank you very much!










share|improve this question
















this is my first post on this website so - sorry for any mistakes.



Given a data frame



xyz <- data.frame(GEO=c("Belgium", "Belgium", "Belgium", "Germany", "Germany", "Germany", "Italy", "Italy", "Italy"),
year=c(2008, 2009, 2010, 2008, 2009, 2010, 2008, 2009, 2010),
median=c(10.3, 45.2, 34.2, 67.8, 68.7, 69.9, 53.2, 43.2, 33.3),
imigration=c(10.2, 45.2, 47.4, 33.3, 44.4, 55.5, 21.5, 76.5, 78.4))


All I want to do is to count avg index of change for median and imigration.



index=(x[n]/x[1])^(1/(n-1)) 


I tried a lot of solutions but I couldn't find the good one.
This is my code - maybe you will use this to help me.



First:



groupColumns = c("GEO","year")
dataColumns = c("median", "imigration")
index_median = ddply(mediana2, groupColumns, function(x) x[c((nrow(x)/x[1])^(1/nrow(x)-1)), ])
head(index_median)


Second:



xyz_index <- aggregate(xyz$median, xyz$imigration by=list(Category=xyz$"GEO/TIME"), FUN=median$index_median[nrow(median)]/median[1])^(1/(nrow(median)-1))


I want to get avg index of change for median and imigration (index_median, index_imigration) for every country (Belgium, Germany, Italy).
Something like that (numbers are fake):



 GEO index_median index_imigration
1 Germany 2.3 1.3
2 Italy 1.6 1.6
3 Belgium 1.2 2.0


Thank you very much!







r grouping median






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 11:28







wloczykijek

















asked Mar 27 at 10:52









wloczykijekwloczykijek

112 bronze badges




112 bronze badges










  • 1





    Can you edit your question and include your expected output: i.e. what should your result(s) look like?

    – Phil
    Mar 27 at 11:06











  • Thank you for your advice, done!

    – wloczykijek
    Mar 27 at 11:10











  • @wloczykijek Welcome to SO I think you should read this. Meanwhile, do you want something like library(dplyr);xyz %>% group_by(GEO) %>% mutate(Ind=((median/first(median))^(1/(row_number()-1))))

    – A. Suliman
    Mar 27 at 11:15






  • 1





    @A.Suliman I edited my post - you can see what exactly I want to get

    – wloczykijek
    Mar 27 at 11:29






  • 1





    Now is better, one last question what n stand for in index=(x[n]/x[1])^(1/(n-1)), I think it's the last obs e.g. for Belgium's median it's 34.2

    – A. Suliman
    Mar 27 at 11:32













  • 1





    Can you edit your question and include your expected output: i.e. what should your result(s) look like?

    – Phil
    Mar 27 at 11:06











  • Thank you for your advice, done!

    – wloczykijek
    Mar 27 at 11:10











  • @wloczykijek Welcome to SO I think you should read this. Meanwhile, do you want something like library(dplyr);xyz %>% group_by(GEO) %>% mutate(Ind=((median/first(median))^(1/(row_number()-1))))

    – A. Suliman
    Mar 27 at 11:15






  • 1





    @A.Suliman I edited my post - you can see what exactly I want to get

    – wloczykijek
    Mar 27 at 11:29






  • 1





    Now is better, one last question what n stand for in index=(x[n]/x[1])^(1/(n-1)), I think it's the last obs e.g. for Belgium's median it's 34.2

    – A. Suliman
    Mar 27 at 11:32








1




1





Can you edit your question and include your expected output: i.e. what should your result(s) look like?

– Phil
Mar 27 at 11:06





Can you edit your question and include your expected output: i.e. what should your result(s) look like?

– Phil
Mar 27 at 11:06













Thank you for your advice, done!

– wloczykijek
Mar 27 at 11:10





Thank you for your advice, done!

– wloczykijek
Mar 27 at 11:10













@wloczykijek Welcome to SO I think you should read this. Meanwhile, do you want something like library(dplyr);xyz %>% group_by(GEO) %>% mutate(Ind=((median/first(median))^(1/(row_number()-1))))

– A. Suliman
Mar 27 at 11:15





@wloczykijek Welcome to SO I think you should read this. Meanwhile, do you want something like library(dplyr);xyz %>% group_by(GEO) %>% mutate(Ind=((median/first(median))^(1/(row_number()-1))))

– A. Suliman
Mar 27 at 11:15




1




1





@A.Suliman I edited my post - you can see what exactly I want to get

– wloczykijek
Mar 27 at 11:29





@A.Suliman I edited my post - you can see what exactly I want to get

– wloczykijek
Mar 27 at 11:29




1




1





Now is better, one last question what n stand for in index=(x[n]/x[1])^(1/(n-1)), I think it's the last obs e.g. for Belgium's median it's 34.2

– A. Suliman
Mar 27 at 11:32






Now is better, one last question what n stand for in index=(x[n]/x[1])^(1/(n-1)), I think it's the last obs e.g. for Belgium's median it's 34.2

– A. Suliman
Mar 27 at 11:32













2 Answers
2






active

oldest

votes


















0














We can dplyr::group_by GEO then take the first and last observation for median and immigration using dplyr::first and last in each group. To get number of observations in each group we will use dplyr::n()



library(dplyr)
xyz %>% group_by(GEO) %>%
#medain[n()] similar to last(median)
summarise(Ind_median=((last(median)/first(median))^(1/(n()-1))),
Ind_imigration=((last(imigration)/first(imigration))^(1/(n()-1))))





share|improve this answer
































    0














    You can also use base R only like this:



    avgchange <- function(z, fld) x <- z[,fld]; n<- length(x);(x[n]/x[1])^(1/(n-1)) 


    Argument fld is the field name you want to summarise



    Use base R's by



    z <- by(xyz, list(Category=xyz$GEO), fld="median",FUN=avgchange, simplify=FALSE)
    z


    And this to get a named vector only



    unlist(z)


    And similarly for the imigration column



    z <- by(xyz, list(Category=xyz$GEO), fld="imigration",FUN=avgchange, simplify=FALSE)
    z
    unlist(z)





    share|improve this answer



























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55375439%2fis-there-an-r-function-for-finding-the-avg-index-of-change-xn-x11-n-1%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      We can dplyr::group_by GEO then take the first and last observation for median and immigration using dplyr::first and last in each group. To get number of observations in each group we will use dplyr::n()



      library(dplyr)
      xyz %>% group_by(GEO) %>%
      #medain[n()] similar to last(median)
      summarise(Ind_median=((last(median)/first(median))^(1/(n()-1))),
      Ind_imigration=((last(imigration)/first(imigration))^(1/(n()-1))))





      share|improve this answer





























        0














        We can dplyr::group_by GEO then take the first and last observation for median and immigration using dplyr::first and last in each group. To get number of observations in each group we will use dplyr::n()



        library(dplyr)
        xyz %>% group_by(GEO) %>%
        #medain[n()] similar to last(median)
        summarise(Ind_median=((last(median)/first(median))^(1/(n()-1))),
        Ind_imigration=((last(imigration)/first(imigration))^(1/(n()-1))))





        share|improve this answer



























          0












          0








          0







          We can dplyr::group_by GEO then take the first and last observation for median and immigration using dplyr::first and last in each group. To get number of observations in each group we will use dplyr::n()



          library(dplyr)
          xyz %>% group_by(GEO) %>%
          #medain[n()] similar to last(median)
          summarise(Ind_median=((last(median)/first(median))^(1/(n()-1))),
          Ind_imigration=((last(imigration)/first(imigration))^(1/(n()-1))))





          share|improve this answer













          We can dplyr::group_by GEO then take the first and last observation for median and immigration using dplyr::first and last in each group. To get number of observations in each group we will use dplyr::n()



          library(dplyr)
          xyz %>% group_by(GEO) %>%
          #medain[n()] similar to last(median)
          summarise(Ind_median=((last(median)/first(median))^(1/(n()-1))),
          Ind_imigration=((last(imigration)/first(imigration))^(1/(n()-1))))






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 27 at 11:45









          A. SulimanA. Suliman

          7,4914 gold badges13 silver badges26 bronze badges




          7,4914 gold badges13 silver badges26 bronze badges


























              0














              You can also use base R only like this:



              avgchange <- function(z, fld) x <- z[,fld]; n<- length(x);(x[n]/x[1])^(1/(n-1)) 


              Argument fld is the field name you want to summarise



              Use base R's by



              z <- by(xyz, list(Category=xyz$GEO), fld="median",FUN=avgchange, simplify=FALSE)
              z


              And this to get a named vector only



              unlist(z)


              And similarly for the imigration column



              z <- by(xyz, list(Category=xyz$GEO), fld="imigration",FUN=avgchange, simplify=FALSE)
              z
              unlist(z)





              share|improve this answer





























                0














                You can also use base R only like this:



                avgchange <- function(z, fld) x <- z[,fld]; n<- length(x);(x[n]/x[1])^(1/(n-1)) 


                Argument fld is the field name you want to summarise



                Use base R's by



                z <- by(xyz, list(Category=xyz$GEO), fld="median",FUN=avgchange, simplify=FALSE)
                z


                And this to get a named vector only



                unlist(z)


                And similarly for the imigration column



                z <- by(xyz, list(Category=xyz$GEO), fld="imigration",FUN=avgchange, simplify=FALSE)
                z
                unlist(z)





                share|improve this answer



























                  0












                  0








                  0







                  You can also use base R only like this:



                  avgchange <- function(z, fld) x <- z[,fld]; n<- length(x);(x[n]/x[1])^(1/(n-1)) 


                  Argument fld is the field name you want to summarise



                  Use base R's by



                  z <- by(xyz, list(Category=xyz$GEO), fld="median",FUN=avgchange, simplify=FALSE)
                  z


                  And this to get a named vector only



                  unlist(z)


                  And similarly for the imigration column



                  z <- by(xyz, list(Category=xyz$GEO), fld="imigration",FUN=avgchange, simplify=FALSE)
                  z
                  unlist(z)





                  share|improve this answer













                  You can also use base R only like this:



                  avgchange <- function(z, fld) x <- z[,fld]; n<- length(x);(x[n]/x[1])^(1/(n-1)) 


                  Argument fld is the field name you want to summarise



                  Use base R's by



                  z <- by(xyz, list(Category=xyz$GEO), fld="median",FUN=avgchange, simplify=FALSE)
                  z


                  And this to get a named vector only



                  unlist(z)


                  And similarly for the imigration column



                  z <- by(xyz, list(Category=xyz$GEO), fld="imigration",FUN=avgchange, simplify=FALSE)
                  z
                  unlist(z)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 27 at 14:00









                  BhasBhas

                  1,5211 gold badge9 silver badges8 bronze badges




                  1,5211 gold badge9 silver badges8 bronze badges






























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55375439%2fis-there-an-r-function-for-finding-the-avg-index-of-change-xn-x11-n-1%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript