dataframe - Conditional replacement of values in data frame column based off multiple other columns - R -


my data frame looks this

> tornado_frame          tornado_names level      value 1     node per cluster   low  -34.72222 2          tb per node   low  -52.08333 3  expense per cluster   low -104.16667 4             total tb   low  -62.50000 5  revenue per cluster   low  -52.08333 6     node per cluster  high   20.83333 7          tb per node  high   41.66667 8  expense per cluster  high   52.08333 9             total tb  high  145.83333 10 revenue per cluster  high  156.25000 

i want table transform this

> tornado_frame          tornado_names level      value 1     node per cluster   low   34.72222 2          tb per node   low   52.08333 3  expense per cluster   low  104.16667 4             total tb   low  -62.50000 5  revenue per cluster   low  -52.08333 6     node per cluster  high  -20.83333 7          tb per node  high  -41.66667 8  expense per cluster  high  -52.08333 9             total tb  high  145.83333 10 revenue per cluster  high  156.25000 

where negative sign in "value" changes if absolute value greater of "high" level column , of same tornado_name column.

i tried few nested if's got messy me. appreciated!

here data:

> dput(tornado_frame) structure(list(tornado_names = structure(c(2l, 4l, 1l, 5l, 3l,  2l, 4l, 1l, 5l, 3l), .label = c("expense per cluster", "node per cluster",  "revenue per cluster", "tb per node", "total tb"), class = "factor"),      level = structure(c(2l, 2l, 2l, 2l, 2l, 1l, 1l, 1l, 1l, 1l     ), .label = c("high", "low"), class = "factor"), value = c(34.72222,      52.08333, 104.16667, -62.5, -52.08333, -20.83333, -41.66667,      -52.08333, 145.83333, 156.25)), .names = c("tornado_names",  "level", "value"), class = "data.frame", row.names = c(na, -10l )) 

here's possible data.table solution

library(data.table) setdt(df)[, value := if(diff(abs(value)) < 0) value * -1,                                             = tornado_names] df #           tornado_names level     value #  1:    node per cluster   low  34.72222 #  2:         tb per node   low  52.08333 #  3: expense per cluster   low 104.16667 #  4:            total tb   low -62.50000 #  5: revenue per cluster   low -52.08333 #  6:    node per cluster  high -20.83333 #  7:         tb per node  high -41.66667 #  8: expense per cluster  high -52.08333 #  9:            total tb  high 145.83333 # 10: revenue per cluster  high 156.25000 

this check condition per tornado_names , change sign values within groups condition satisfied.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -