1

Data example.

date1 = seq(as.Date("2019/01/01"), by = "month", length.out = 29)
date2= seq(as.Date("2019/01/01"), by = "month", length.out = 29)
date3 = seq(as.Date("2019/01/01"), by = "month", length.out = 29)
date4 = seq(as.Date("2019/01/01"), by = "month", length.out = 10)

subproducts1=rep("1",29)
subproducts2=rep("2",29)
subproductsx=rep("x",29)
subproductsy=rep("y",10)

b1 <- c(rnorm(29,5))
b2 <- c(rnorm(29,5))
b3 <-c(rnorm(29,5))
b4 <- c(rnorm(10,5))


dfone <- data.frame("date"= c(date1,date2,date3,date4),
                "subproduct"= 
                  c(subproducts1,subproducts2,subproductsx,subproductsy),
                "actuals"= c(b1,b2,b3,b4))

Question: How would I split dfone into 4 dataframes in global environment based off unique subproducts?

chriswang123456
  • 435
  • 2
  • 10
  • 1
    I would strongly discourage you from dumping variables into your global environment that have indexes in their names. It makes a lot of things much harder to work with in R. It's so much easier to keep related data objects in lists. For example you could just use `split()` on `dfone`, like `split(dfone, dfone$subproduct)` – MrFlick Jul 14 '21 at 19:11
  • 1
    `split(dfone, dfone$subproduct)`. Then, use `list2env` but pay atention no @MrFlick's comment. – Rui Barradas Jul 14 '21 at 19:13
  • Can someone explain to me why putting objects into glb environment is bad practice? In my case I am trying to automate/clarify code so it's easier for someone to look at or use in the future – chriswang123456 Jul 14 '21 at 19:18
  • The data will always be changing, although the format will be the same and the amount of modifications I need to do manually can be varying too – chriswang123456 Jul 14 '21 at 19:27

1 Answers1

3

We can use group_split from dplyr to return a list of data.frame/tibbles

library(dplyr)
dfone %>%
    group_split(subproduct)

It may be better to split into a list and do the transformations within the list. But, global objects can be created by looping over the sequence of unique 'subproduct' elements, and then assign new objects ('subprod1', 'subprod2' ...) on the subset of data for that particular 'subproduct'

un1 <- unique(dfone$subproduct)
for(i in seq_along(un1)) 
     assign(paste0('subprod', i), subset(dfone, subproduct == un1[i]))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Can we also add these to global environment with some sort of name? Like subproduct1,subproduct2.. etc – chriswang123456 Jul 14 '21 at 19:21
  • 1
    @chriswang123456 do you want the updated one – akrun Jul 14 '21 at 19:23
  • In my real data, the names of subproduct are very long, is there a way to make custom names like subprod1 ... subprod100 – chriswang123456 Jul 14 '21 at 19:26
  • 1
    @chriswang123456 try the update – akrun Jul 14 '21 at 19:27
  • Can you go to the comments that I posted above, not to this answer? In the case of automating/clarifying code for someone else to use... Wouldn't creating a bunch of list objects be difficult for someone who has never seen the code to use? If they wanted to reuse things and data will always be changing I think making global environment variables is the best solution.. Wanted to hear your thoughts – chriswang123456 Jul 14 '21 at 19:32
  • 1
    @chriswang123456 If you are creating 100s of objects in the global env, not a good choice. Reasons - 1) you are creating this objects and then gathering those objects by applying some function. The same can be done in a `list` if you loop over the list with `lapply` and apply the function. 2) Once the transformations are done, you may need to gather all objects again for writing back. Here, also from a list it is easier. Having said that it is all about your convenience. If you are okay with global objects, go for it. But, I wouldn't do that – akrun Jul 14 '21 at 19:35
  • 1
    I see. Currently if I give my code to someone else with list based solutions I don't think it would be obvious for them.. but maybe I'm wrong. In the future I will work on making better list based solutions. Thanks for your wisdom and advice. – chriswang123456 Jul 14 '21 at 19:44