简体   繁体   中英

Creating new aggregated R dataframes with a function in lapply

I want to create new aggregated dataframes from existing ones that are collected in a list. Ideally they would appear as their own dataframes indicated by a prefix. This is where I've got:

dfList <- list(A = data.frame(y = sample(1:100), x = c("grp1","grp2")),
               B = data.frame(y = sample(1:100), x = c("grp1","grp2")))

agrFun <- function(df){
    prefix <- deparse(substitute(df))
    assign(paste0(prefix,"_AGR"),
           aggregate(y ~ x, data = df, sum))
}

lapply(seq_along(dfList), function(x) agrFun(dfList[[x]]))

Aggregation happens as intended but I'm not sure what I need to do otherwise in order to create dataframes A_AGR and B_AGR.

EDIT: A bit of clarification. I want to have the aggregated dataframes appear in the environment.

So instead of this

ls() [1] "agrFun" "dfList"

my goal is to have

ls() [1] "A_AGR" "B_AGR" "agrFun" "dfList"

EDIT2:

Or more ideal would be to have dfList include dataframes A, A_AGR, B and B_AGR after the process.

EDIT3:

And I also want to preserve the names of the dataframes.

Your way to do seems extremely complicated. You can create the wanted named data.frames in a list with this one liner:

setNames(lapply(dfList, function(u) aggregate(y~x, u, sum)), paste0(names(dfList),"_AGR"))

#$A_AGR
#     x    y
#1 grp1 2340
#2 grp2 2710

#$B_AGR
#     x    y
#1 grp1 2573
#2 grp2 2477

With your function agrFun :

lst = setNames(lapply(dfList, function(x) agrFun(x)), paste0(names(dfList),"_AGR"))

If you want to append the two lists:

dfList = append(lst, dfList)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM