Processing the list of data.frames with “apply” family of functions

前端 未结 2 1531
栀梦
栀梦 2021-01-25 13:12

I have a data frame which I then split into three (or any number) of dataframes.

What I’m trying to do is to automatically process each column in each dataframe and add

2条回答
  •  半阙折子戏
    2021-01-25 13:39

    Use the plyr package to do all of this one step:

    library(plyr)
    clag <- function(x, n=1)c(rep(NA, n), head(x, -n))
    x <- ddply(dd, .(dim1), transform, 
                   v1.lag=clag(v1), v2.lag=clag(v2), v3.lag=clag(v3))
    
    head(x)
              v1         v2         v3 dim1 dim2     v1.lag     v2.lag     v3.lag
    1  0.4465910 -0.2564334 -0.9122640    A    J         NA         NA         NA
    2 -0.3748563 -0.9461061  0.1641274    A    J  0.4465910 -0.2564334 -0.9122640
    3 -0.5010834 -0.4413026 -0.7509968    A    J -0.3748563 -0.9461061  0.1641274
    4 -0.5278584 -0.6377017  0.5528831    A    J -0.5010834 -0.4413026 -0.7509968
    5 -0.4290586  0.4687849  0.6885102    A    J -0.5278584 -0.6377017  0.5528831
    6  0.1179935 -0.2742456 -0.1945482    A    J -0.4290586  0.4687849  0.6885102
    

提交回复
热议问题