Finding Growth in Dataframe in R

前端 未结 2 1068
忘了有多久
忘了有多久 2021-01-03 05:21

Suppose I have the following data frame

Website <- rep(paste(\"Website\",1:3),2)
Year <- c(rep(2013,3),rep(2014,3))
V1 <- c(10,20,50,20,30,70)
V2 &l         


        
2条回答
  •  旧巷少年郎
    2021-01-03 05:51

    A data.table option (I am using data.table_1.9.5 that introduced the function shift). Assuming that the year column is "ordered", convert the "data.frame" to "data.table" using setDT, loop through the columns ("V1", "V2") with lapply (specify the columns in .SDcols) and do the calculation for individual columns (x/shift(x)...). The default setting for shift is type='lag' and n=1L. If you want to remove the NA rows, you can use na.omit which is also fast in the devel version.

    library(data.table)
    na.omit(setDT(df)[, lapply(.SD, function(x)
                  x/shift(x) - 1), by=Website, .SDcols=3:4])
    #     Website  V1  V2
    #1: Website 1 1.0 2.0
    #2: Website 2 0.5 1.0
    #3: Website 3 0.4 0.5
    

提交回复
热议问题