[R] Create New Column Inside Data Frame for Many Data Frames

Rui Barradas ruipbarradas at sapo.pt
Sun Apr 14 19:36:44 CEST 2013


Hello,

I'm not completely sure I've understood. Your variable 'letters' iholds 
the names of the data.frames? If so it's better if you put yoyr 
data.frames in a list and then use that list. Something like


lst <- list(A, B)

for (i in seq_along(lst)){
	lst[[i]][["Rate"]] <- ROC(lst[[i]][["population"]])
}


Hope this helps,

Rui Barradas

Em 14-04-2013 18:19, Sparks, John James escreveu:
> Dear R Helpers,
>
> I have a large number of data frames and I need to create a new column
> inside each data frame.  Because there is a large number, I need to "loop"
> through this, but I don't know the syntax of assigning a new column name
> dynamically.
>
> Below is a simple example of what I need to do.  Assume that I have to do
> this for all 26 letters and you should see the form of the problem.
>
> Any help would be much appreciated.  If more information is needed, please
> let me know.
>
> Many thanks.
> --John Sparks
>
>
>
> library(quantmod)
> A <- data.frame(population=c(100, 300, 5000, 2000, 900, 2500))
> A$Rate<-ROC(A["population"])
>
> B <- data.frame(population=c(200, 300, 4000, 3000, 2000, 500))
> B$Rate<-ROC(B["population"])
>
> letters<-c("A","B")
> length(letters)
>
> #for (i in letters){
> # HELP!
> #}
>
> ______________________________________________
> R-help at 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.
>



More information about the R-help mailing list