Hi TLowe,
I'm not quite sure if I understand what you are trying to do. If you
are trying to get the cumulative sum of your data frame along each
column you can simply do
rcumsum=function(x){cumsum(x)/sum(x)}
apply(tdat,2,rcumsum)
Yet that is not what your code is doing. With a bit of
Hey Folks,
Could somebody help me rewrite the following code?
I am looping through all records across 5 fields to calculate the cumulative
percentage of each record (relative to each individual field).
Is there a way to rewrite it so I don't have to loop through each individual
record?
#
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
Is this basically what you want to do? (Please include commented,
minimal, self-contained, reproducible code so we don't have to guess
at what you want)
x - data.frame(a=runif(10), b=runif(10))
# do for one column
cumsum(x$a)/sum(x$a)
[1] 0.05892073 0.08129611 0.11067218 0.28640268 0.28969826
Thank you all. That's exactly what I was looking for.
TLowe wrote:
Hey Folks,
Could somebody help me rewrite the following code?
I am looping through all records across 5 fields to calculate the
cumulative
percentage of each record (relative to each individual field).
Is there a
: [R] Help rewriting looping structure?
Thank you all. That's exactly what I was looking for.
TLowe wrote:
Hey Folks,
Could somebody help me rewrite the following code?
I am looping through all records across 5 fields to calculate the
cumulative
percentage of each record (relative
6 matches
Mail list logo