Steven Kang
2010-Jul-16 01:18 UTC
[R] Storing processed results back into original objects
Hi all, There are matrices with same column names but arranged in different orders and I desire columns of these matrices to have same order. For example, below are 2 arbitrary data sets with columns arranged in different order. I require columns of these to have same order as specified in "columns" object and the results stored in the original object names. I know this can be done simply by: D1 <- D1[, columns] But if there are hundreds of matrices, then more efficient method is required. columns <- c("A", "B", "C") D1 <- matrix(rnorm(6), nrow = 2, dimnames = list(c("R1", "R2"), c("C", "A", "B"))) D2 <- matrix(rnorm(6), nrow = 2, dimnames = list(c("R1", "R2"), c("C", "B", "A")))> D1C A B R1 -0.653978178594122 -0.15910510749630 0.90507729153852 R2 0.015557641181675 -0.73944224596032 0.23484927168787> D2C B A R1 0.18843559757623 0.207589297797905 -0.018884844424975 R2 1.87387725184456 0.050349118287824 -1.796404635019739 Dlist <- list(D1, D2) lapply(Dlist, function(x) x[, columns]) [[1]] A B C R1 -0.15910510749630 0.90507729153852 -0.653978178594122 R2 -0.73944224596032 0.23484927168787 0.015557641181675 [[2]] A B C R1 -0.018884844424975 0.207589297797905 0.18843559757623 R2 -1.796404635019739 0.050349118287824 1.87387725184456 How can the results from the lapply function be stored back into the original object names? Many thanks. -- Steven [[alternative HTML version deleted]]
Dennis Murphy
2010-Jul-16 01:46 UTC
[R] Storing processed results back into original objects
Hi: This seems to work: b <- lapply(Dlist, function(x) x[, columns]) dnames <- c('D1', 'D2') for(i in seq_along(dnames)) assign(dnames[i], b[[i]]) HTH, Dennis On Thu, Jul 15, 2010 at 6:18 PM, Steven Kang <stochastickang@gmail.com>wrote:> Hi all, > > There are matrices with same column names but arranged in different orders > and I desire columns of these matrices to have same order. > > For example, below are 2 arbitrary data sets with columns arranged in > different order. I require columns of these to have same order as specified > in "columns" object and the results stored in the original object names. > I know this can be done simply by: > D1 <- D1[, columns] > > But if there are hundreds of matrices, then more efficient method is > required. > > columns <- c("A", "B", "C") > D1 <- matrix(rnorm(6), nrow = 2, dimnames = list(c("R1", "R2"), c("C", "A", > "B"))) > D2 <- matrix(rnorm(6), nrow = 2, dimnames = list(c("R1", "R2"), c("C", "B", > "A"))) > > > D1 > C A B > R1 -0.653978178594122 -0.15910510749630 0.90507729153852 > R2 0.015557641181675 -0.73944224596032 0.23484927168787 > > D2 > C B A > R1 0.18843559757623 0.207589297797905 -0.018884844424975 > R2 1.87387725184456 0.050349118287824 -1.796404635019739 > > Dlist <- list(D1, D2) > lapply(Dlist, function(x) x[, columns]) > > [[1]] > A B C > R1 -0.15910510749630 0.90507729153852 -0.653978178594122 > R2 -0.73944224596032 0.23484927168787 0.015557641181675 > [[2]] > A B C > R1 -0.018884844424975 0.207589297797905 0.18843559757623 > R2 -1.796404635019739 0.050349118287824 1.87387725184456 > How can the results from the lapply function be stored back into the > original object names? > > Many thanks. > > > > -- > Steven > > [[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. >[[alternative HTML version deleted]]
Jonathan Christensen
2010-Jul-16 01:51 UTC
[R] Storing processed results back into original objects
Steven, You can do it with assign() if you keep the names when you put the items in the list: Dlist <- list(D1=D1, D2=D2) # put the names of the objects in the list Newlist <- lapply(Dlist, function(x) x[, columns]) # create a new list with the output for(i in seq(length(Newlist))) { assign(names(Newlist)[i],Newlist[[i]]) # assign the new objects to the original names } You may want to keep in mind, though:> fortune(236)The only people who should use the assign function are those who fully understand why you should never use the assign function. -- Gregory L. Snow R-help (July 2009) You might want to ask yourself whether this is really the best way to achieve what you want to do. Jonathan On Thu, Jul 15, 2010 at 7:18 PM, Steven Kang <stochastickang@gmail.com>wrote:> Hi all, > > There are matrices with same column names but arranged in different orders > and I desire columns of these matrices to have same order. > > For example, below are 2 arbitrary data sets with columns arranged in > different order. I require columns of these to have same order as specified > in "columns" object and the results stored in the original object names. > I know this can be done simply by: > D1 <- D1[, columns] > > But if there are hundreds of matrices, then more efficient method is > required. > > columns <- c("A", "B", "C") > D1 <- matrix(rnorm(6), nrow = 2, dimnames = list(c("R1", "R2"), c("C", "A", > "B"))) > D2 <- matrix(rnorm(6), nrow = 2, dimnames = list(c("R1", "R2"), c("C", "B", > "A"))) > > > D1 > C A B > R1 -0.653978178594122 -0.15910510749630 0.90507729153852 > R2 0.015557641181675 -0.73944224596032 0.23484927168787 > > D2 > C B A > R1 0.18843559757623 0.207589297797905 -0.018884844424975 > R2 1.87387725184456 0.050349118287824 -1.796404635019739 > > Dlist <- list(D1, D2) > lapply(Dlist, function(x) x[, columns]) > > [[1]] > A B C > R1 -0.15910510749630 0.90507729153852 -0.653978178594122 > R2 -0.73944224596032 0.23484927168787 0.015557641181675 > [[2]] > A B C > R1 -0.018884844424975 0.207589297797905 0.18843559757623 > R2 -1.796404635019739 0.050349118287824 1.87387725184456 > How can the results from the lapply function be stored back into the > original object names? > > Many thanks. > > > > -- > Steven > > [[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. >[[alternative HTML version deleted]]