Friday, October 7, 2022

[FIXED] How do I Divide Values in a Column by the Value in the Last Cell?

Issue

So I am working in R, and I am trying to write a function. But I reached a roadblock.

I have the following df:

Number Freq CumFreq
00.1 2 2
00.5 1 3

...so on and so forth.

I'd like to have it so the numbers in df$CumFreq are divided by the last cell in the df$CumFreq, and create a new column. For instance:

Number Freq CumFreq CF/n
00.1 2 2 0.667
00.5 1 3 1

Any advice on how to write this out?


Solution

You can try

df$CFn <- with(df,CumFreq/sum(Freq))

or

df$CFn <- with(df,CumFreq/tail(CumFreq,1))


Answered By - ThomasIsCoding
Answer Checked By - Pedro (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.