Adding a column to a dataframe in R

前端 未结 2 1109
野的像风
野的像风 2021-01-30 00:56

I have the following dataframe (df)

 start     end
1    14379   32094
2   151884  174367
3   438422  449382
4   618123  621256
5   698271  714321
6          


        
2条回答
  •  星月不相逢
    2021-01-30 01:19

    Even if that's a 7 years old question, people new to R should consider using the data.table, package.

    A data.table is a data.frame so all you can do for/to a data.frame you can also do. But many think are ORDERS of magnitude faster with data.table.

    vec <- 1:10
    library(data.table)
    DT <- data.table(start=c(1,3,5,7), end=c(2,6,7,9))
    DT[,new:=apply(DT,1,function(row) mean(vec[ row[1] : row[2] ] ))]
    

提交回复
热议问题