1

I have a dataset called "weights" and I was wondering what is the most efficient way to write the following code:

library(d3treeR)
library(treemap)
library(dplyr)

myfun <- function(x){
 x <- weights %>% filter(Region %in% x) 
}

CAN<-myfun("Canada")
ON<-myfun("Ontario")
NL<-myfun("Newfoundland and Labrador")
PE<-myfun("Prince Edward Island")
NS<-myfun("Nova Scotia")
NB<-myfun("New Brunswick")
QC<-myfun("Quebec")

CAN=treemap(CAN, index=c("Level.0","Level.1"), vSize="X2015", type="index")
ON=treemap(ON, index=c("Level.0","Level.1"), vSize="X2015", type="index")
NL=treemap(NL, index=c("Level.0","Level.1"), vSize="X2015", type="index")
PE=treemap(PE, index=c("Level.0","Level.1"), vSize="X2015", type="index")
NS=treemap(NS, index=c("Level.0","Level.1"), vSize="X2015", type="index")
NB=treemap(NB, index=c("Level.0","Level.1"), vSize="X2015", type="index")
QC=treemap(QC, index=c("Level.0","Level.1"), vSize="X2015", type="index")

Thank you. I have been trying to use a for loop with unique(weights$Region) to populate these 7 dataframes.

Antarqui
  • 467
  • 6
  • 18
  • Can you please share a [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) copy of your data? – Conor Neilson Aug 11 '18 at 23:33
  • If those 7 regions are are all of the regions, you can use `split()` to cut your dataframe into smaller dataframes (each smaller dataframe is stored as a list element). Then you can `lapply()` your treemap variable to each: – DanY Aug 11 '18 at 23:52
  • Thanks Dan, that worked perfectly ! – Antarqui Aug 12 '18 at 00:10

3 Answers3

2

Succinctly, split your data into subsets by region and lapply the treemap function to each:

mylist <- split(weights, weights$Region)
result <- lapply(mylist, treemap::treemap, index=c("Level.0","Level.1"), vSize="X2015", type="index")

Update: Yikes! The question asked for "efficient" way to write code and my 2-liner with lapply lost out to an answer with 2 for loops, a cbind, and an assign. Well, what can you do... :)

DanY
  • 5,920
  • 1
  • 13
  • 33
1

As others stated, hard to figure out, also the d3treeR package is not available on CRAN, so I'm not sure if this would work. Using purrr::map()

library(tidyverse)
library(d3treeR)

myfun <- function(weights, Region, x){
  weights %>% 
    filter(Region %in% x) %>%
    treemap(index=c("Level.0","Level.1"), vSize="X2015", type="index")
}

Regions <- c("Canada", "Ontario", "Newfoundland and Labrador", "Prince Edward Island", "Nova Scotia", "New Brunswick", "Quebec")

results <- map(Regions, myfun, weights = weights, Region = Region)
Phil
  • 7,287
  • 3
  • 36
  • 66
1

something like this:

    x <- c("CAN",  "ON",  "NL", "PE", "NS", "NB", "QC")
    weights <- as.data.frame(cbind(weights=rep(c(1,2,3), 7), Region=x))

    for(i in x){
      y <- weights[weights$Region == i, ]
      assign(i, y) 
    } 

in you case it would be

x <- c("CAN",  "ON",  "NL", "PE", "NS", "NB", "QC")     
for(i in x){
          y <- treemap(weights[weights$Region == i, ], index=c("Level.0","Level.1"), vSize="X2015", type="index")
          assign(i, y) 
        } 
Nar
  • 648
  • 4
  • 8