Tuesday 15 September 2015

r - how to use apply-like function on data frame? [please see details below] -



r - how to use apply-like function on data frame? [please see details below] -

i have dataframe columns a, b , c. want apply function on each row of dataframe in function check value of row$a , row$b , update row$c based on values. how can accomplish that?

example:

b c 1 1 10 10 2 2 20 20 3 na 30 30 4 na 40 40 5 5 50 50

now want update rows in c column b/2 value in same row if value in column row na.

so dataframe after changes like:

b c 1 1 10 10 2 2 20 20 3 na 30 15 4 na 40 20 5 5 50 50

i know if can done without using for loop.

try this:

your_data <- within(your_data, c[is.na(a)] <- b[is.na(a)] / 2)

r data.frame apply

No comments:

Post a Comment