0
votes

ID Type Sales Date 1 1 $ 5,027 18-Jan-2016
2 1 $ 2,646 10-Nov-2012
3 1 $ 7,549 11-Feb-2018
4 2 $ 4,536 18-Feb-2016
5 2 $ 3,118 26-Aug-2017
6 3 $ 9,815 07-Jun-2017
7 3 $ 885 15-Dec-2017
8 3 $ 2,911 10-Nov-2017
9 3 $ 1,823 12-Oct-2015
10 4 $ 5,723 04-Jul-2014
11 5 $ 2,612 31-Mar-2015
12 5 $ 3,344 06-Jan-2016
13 5 $ 4,215 22-May-2016
14 6 $ 5,500 23-Mar-2018

To split the above dataset (Main) into Type wise, we may use the following macro. How to do the same in R. Thanks in advance.

%MACRO split;  

%DO m = 1 %TO 6 ;  


DATA type_%eval(&m) ;  


SET main ;  


IF Type = &m then output type_%eval(&m) ;  


RUN ;  

%END ;  

%MEND split ;  

%split ;  

ID Type Sales Date
1 1 $ 5,027 18-Jan-2016
2 1 $ 2,646 10-Nov-2012
3 1 $ 7,549 11-Feb-2018

ID Type Sales Date
4 2 $ 4,536 18-Feb-2016
5 2 $ 3,118 26-Aug-2017

ID Type Sales Date
6 3 $ 9,815 07-Jun-2017
7 3 $ 885 15-Dec-2017
8 3 $ 2,911 10-Nov-2017
9 3 $ 1,823 12-Oct-2015

this will give me following datasets Type1, Type2, Type3 ..... Type6

1

1 Answers

0
votes

You can use split. If your data frame is called df, do

df.list <- split(df, df$Type)

This gives you a list of data frames. You can get individual data frames by using $ and the value of Type (as below). Since these names don't follow the convention of not starting with a number, you have to put ticks or quotes around them

df.list$'1'

You can also use the list indexing e.g. df.list[3] for the third data.frame. In your example, by coincidence, these align at times e.g. df.list$'1' is the same as df.list[1].