Hello all,

This seems like a pretty standard question - suppose I want to loop through
a set of similar data-frames, with similar variables, and create new
variables within them:

nl<-seq(1,5)for (i in nl) {
  assign(paste0("df_",nl[i]),data.frame(x=seq(1:10),y=rnorm(10)))}
ls()[grep("df_",ls())]
nls<-ls()[grep("df_",ls())]for (df in nls) {
  print(df)
  for (var in names(get(df))) {
    print(var)
    assign(paste0(df,"$",paste0(var,"_cs")),cumsum(get(df)[[var]]))
  }}
ls()[grep("df_",ls())]

The code above *almost* works, except that it creates a whole bunch of
objects of the form df_1$x_cs,df_1$yx_cs ..... What I want is 5 dataframes,
with the $ elements enclosed, as usual.

Any help or guidance would be appreciated.

Much thanks,
Dan

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to