Skip to content
Prev 131455 / 398502 Next

Help rewriting looping structure?

How about this example?

## sample data frame with two columns
df <- data.frame(x = abs(rnorm(20)), y=abs(rnorm(20,2)))

## create new variables in df with an lapply call
df[c("cpctx","cptcty")] <- lapply(df, function(x) cumsum(x)/sum(x))

A possible improvement would be to construct the new column names in
the data frame automatically.

Best,
Erik
TLowe wrote: