Source file in
/prj/hnd/git/dataMgr/Projects/00Demos/2016-03-13-simulateMacros/Problem1/2016-03-13.Rmd
function to go through each file and edit it: result are x1:x5, input would be the file names
x1$group<-factor(x1$group, labels=zQ(a,b,c,d,e))
x1<-data.frame(group=rep(c(1:5),each=2),value=sample(1:100,10))
x2<-data.frame(group=rep(c(1:5),each=2),value=sample(1:100,10))
x3<-data.frame(group=rep(c(1:5),each=2),value=sample(1:100,10))
x4<-data.frame(group=rep(c(1:5),each=2),value=sample(1:100,10))
x5<-data.frame(group=rep(c(1:5),each=2),value=sample(1:100,10))
Two issues: are x1:x5 files or data frames?
First, save each data frame in a file, with a couple of shortcuts based on the regularity of the names.
It's kludgy, but the eval(parse... statement takes the place of the macro notation used by SAS.
dname = '/prj/hnd/git/dataMgr/Projects/00Demos/2016-03-13-simulateMacros/zrdata/'
(fnames = zNameSeq('x', 1:5))
[1] "x1" "x2" "x3" "x4" "x5"
for (k in seq_along(fnames)) eval(parse(text=paste0('save(', fnames[k], ', file="', dname, fnames[k], '.rdata")')))
If the data frames are already loaded, then iterate over the data frame names (regularity helps, but isn't necessary).
(datName = zNameSeq('x', 1:5))
[1] "x1" "x2" "x3" "x4" "x5"
for (k in seq_along(datName)) {
eval(parse(text=paste0(datName[k], '$group<-factor(', datName[k], '$group, labels=zQ(a,b,c,d,e))')))
eval(parse(text=paste0('zQuick(', datName[k], ')')))
}
Dimensions: 10 2
group value
a:2 Min. : 6.00
b:2 1st Qu.:29.50
c:2 Median :55.00
d:2 Mean :52.50
e:2 3rd Qu.:75.75
Max. :95.00
Dimensions: 10 2
group value
a:2 Min. : 9.00
b:2 1st Qu.:19.00
c:2 Median :43.00
d:2 Mean :41.60
e:2 3rd Qu.:54.75
Max. :84.00
Dimensions: 10 2
group value
a:2 Min. : 6.00
b:2 1st Qu.:18.00
c:2 Median :38.50
d:2 Mean :35.50
e:2 3rd Qu.:45.75
Max. :79.00
Dimensions: 10 2
group value
a:2 Min. : 1.00
b:2 1st Qu.: 15.50
c:2 Median : 31.50
d:2 Mean : 40.60
e:2 3rd Qu.: 59.75
Max. :100.00
Dimensions: 10 2
group value
a:2 Min. :22.0
b:2 1st Qu.:45.5
c:2 Median :56.0
d:2 Mean :56.2
e:2 3rd Qu.:74.5
Max. :83.0
Use the list.files function to get the filenames.
Note that this requires the data frame name to be the same as the file names (e.g., x1 in x1.rdata).
(rdaName = list.files(dname, pattern='*.rdata', full.names=T))
[1] "/prj/hnd/git/dataMgr/Projects/00Demos/2016-03-13-simulateMacros/zrdata//x1.rdata" "/prj/hnd/git/dataMgr/Projects/00Demos/2016-03-13-simulateMacros/zrdata//x2.rdata"
[3] "/prj/hnd/git/dataMgr/Projects/00Demos/2016-03-13-simulateMacros/zrdata//x3.rdata" "/prj/hnd/git/dataMgr/Projects/00Demos/2016-03-13-simulateMacros/zrdata//x4.rdata"
[5] "/prj/hnd/git/dataMgr/Projects/00Demos/2016-03-13-simulateMacros/zrdata//x5.rdata"
(datName = sub('\\.rdata', '', list.files(dname, pattern='*.rdata', ignore.case=T)))
[1] "x1" "x2" "x3" "x4" "x5"
for (k in seq_along(rdaName)) {
eval(parse(text=paste0('load("', rdaName[k], '")')))
eval(parse(text=paste0(datName[k], '$group<-factor(', datName[k], '$group, labels=zQ(a,b,c,d,e))')))
eval(parse(text=paste0('zQuick(', datName[k], ')')))
}
Dimensions: 10 2
group value
a:2 Min. : 6.00
b:2 1st Qu.:29.50
c:2 Median :55.00
d:2 Mean :52.50
e:2 3rd Qu.:75.75
Max. :95.00
Dimensions: 10 2
group value
a:2 Min. : 9.00
b:2 1st Qu.:19.00
c:2 Median :43.00
d:2 Mean :41.60
e:2 3rd Qu.:54.75
Max. :84.00
Dimensions: 10 2
group value
a:2 Min. : 6.00
b:2 1st Qu.:18.00
c:2 Median :38.50
d:2 Mean :35.50
e:2 3rd Qu.:45.75
Max. :79.00
Dimensions: 10 2
group value
a:2 Min. : 1.00
b:2 1st Qu.: 15.50
c:2 Median : 31.50
d:2 Mean : 40.60
e:2 3rd Qu.: 59.75
Max. :100.00
Dimensions: 10 2
group value
a:2 Min. :22.0
b:2 1st Qu.:45.5
c:2 Median :56.0
d:2 Mean :56.2
e:2 3rd Qu.:74.5
Max. :83.0
function to go through and create a new file from each of the ones above: result are new1:new5, input would be the original file names
new1<-tapply(x1$value, x1$group, mean)
The solution is above (data frames in files)
There are probably other ways to do this, but this seems the most straightforward for the moment. A true macro maker in R would be useful. Maybe someone has done a package that preprocesses commands with the macro substitutions, but I haven't seen one.