Sparks, John James
2013-Apr-14 17:19 UTC
[R] Create New Column Inside Data Frame for Many Data Frames
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! #}
Rui Barradas
2013-Apr-14 17:36 UTC
[R] Create New Column Inside Data Frame for Many Data Frames
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. >
Jeff Newmiller
2013-Apr-14 17:53 UTC
[R] Create New Column Inside Data Frame for Many Data Frames
I suggest you read the section on indexing in the Introduction to R document that comes with R. In particular, look at the [[i]] notation. This comes in handy in a couple of ways. First, you shouldn't be working with "many" data frames at once that are stored as separately-named objects. If you plan to do similar things to them, then you should store them in a list: myframes <- list() myframes[["A"]] <- data.frame(population=c(100, 300, 5000, 2000, 900, 2500)) Secondly, you can programmatically access the columns in a data frame if needed: i <- "A" dest <- "Rate" myframes[[i]][[dest]]<-ROC(myframes[[i]][["population"]]) Study up on indexing in R... much of the power of this language lies there. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. "Sparks, John James" <jspark4 at uic.edu> wrote:>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.
Hi, ?lapply(LETTERS[1:2],function(x) {x1<-get(x); x1$Rate<- ROC(x1$population);x1}) #[[1]] ?# population?????? Rate #1??????? 100???????? NA #2??????? 300? 1.0986123 #3?????? 5000? 2.8134107 #4?????? 2000 -0.9162907 #5??????? 900 -0.7985077 #6?????? 2500? 1.0216512 #[[2]] ?# population?????? Rate #1 ?????? 200???????? NA #2??????? 300? 0.4054651 #3?????? 4000? 2.5902672 #4?????? 3000 -0.2876821 #5?????? 2000 -0.4054651 #6??????? 500 -1.3862944 #If it is for 26 letters ?lapply(LETTERS,function(x) {x1<-get(x); x1$Rate<- ROC(x1$population);x1}) A.K. ----- Original Message ----- From: "Sparks, John James" <jspark4 at uic.edu> To: r-help at r-project.org Cc: Sent: Sunday, April 14, 2013 1:19 PM Subject: [R] Create New Column Inside Data Frame for Many Data Frames 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.