Christine Chua
Christine Chua

Reputation: 11

For loop does not repeat results in data frame format

I would like to return results from year 2009 to 2017 in a data frame format with each year as column name and rows names as 0.25,0.5 and 0.75 but my code only returns results for year 2009.

for (y in 2009:2017){
  i <- filter(proj, Year.of.Inflow==y)
  y <- quantile(i$Gross.Salary, probs = c(0.25,0.5,0.75))
  return(as.data.frame(y))
}

Would appreciate your advice to my missing code, thanks.

      y
25% 2891.625
50% 3391.860
75% 4402.775

Upvotes: 0

Views: 36

Answers (1)

chinsoon12
chinsoon12

Reputation: 25225

Using only base R functions, you can use the by function to perform functions by groups, then cbind the results list into a data.frame as follows:

do.call(cbind, 
    by(proj$Gross.Salary, proj$Year.of.Inflow, 
        function(x) quantile(x, c(0.25,0.5,0.75)))
)

sample data:

set.seed(0L)
yrs <- rep(2009:2017, each=10)
proj <- data.frame(Year.of.Inflow=yrs, Gross.Salary=runif(length(yrs))*1e6)

output:

        2009     2010     2011     2012     2013     2014     2015     2016
25% 422306.3 249489.7 225912.1 375781.8 490352.4 262665.9 303301.2 357510.0
50% 644955.9 440901.5 384251.0 546553.6 600048.3 457858.4 432948.0 626836.0
75% 897966.6 709969.6 746002.4 709899.9 770278.0 626162.4 607789.2 858114.7
        2017
25% 265454.4
50% 417326.9
75% 712166.3

Upvotes: 1

Related Questions