Yatrosin Yatrosin - 3 years ago 149
R Question

Sum values of dataframe recodifying certain values of original variables on the fly

Let's say that I have the following dataframe:

df <- data.frame(a=c(1,2,3,3,1), b=(c(1,9,1,2,3)),
c=c(1,2,3,3,9), d=(c(1,2,3,9,1)))

I would like to sum the values of a + b + c + d, but every time that any of these variables has a value of 9, I would like to sum 3 instead of 9.

I know I can do this by re-codifying each of the variables using the following syntax:


but I would like to do it with a temporary table or some code that allows me to skip this step. On top of that, I do not want to miss the original value of each variable, because the 9s will have a meaning for other operations that I need to do.

This would be the result that I would like to have:

df$sum <- c(4,9,10,11,8)

Thank you very much,


Answer Source

One option would be to replace elements having values greater than or equal to 9 with 3 and get the rowSums

df$Sum <- rowSums(replace(df[1:4], df[1:4] >= 9, 3))
#[1]  4  9 10 11  8
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download