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.

3
  • Can you please share a reproducible copy of your data? Commented Aug 11, 2018 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: Commented Aug 11, 2018 at 23:52
  • Thanks Dan, that worked perfectly ! Commented Aug 12, 2018 at 0:10

3 Answers 3

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... :)

Sign up to request clarification or add additional context in comments.

3 Comments

You probably have to add as.factor to ensure a proper split :)
split will do the as.factor() for you, but I agree that it's often best to be explicit.
Ok, I ran into the problem of the type being character at some point, but I guess the newest R handles it fine then.
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) 
        } 

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.