R dplyr: mutate specific values with a reference value found in the same column -


how can mutate specific values in column reference value found in same column? data frame looks this:

a  ref  20  s1   12  s2   76  s3   12  s4   12  xy   89 b  ref  02 b  s1   12 b  s2   42 b  s3   21 b  s4   12 b  xy   56 

i mutate having s values divided ref value, not values xy.

basically s1/ref, s2/ref, ... s4/ref , b excluding values xy.

thanks in advance.

here 1 way dplyr. after grouping first column, 'v1', dvide 'v3' divided 'v3' 'v2' 'ref' (assuming there 1 'ref' per each unique 'v1' , replace 'newcol' 'v2' column have values not "s\d+"i.e. "s" followed numbers 'v3' values.

library(dplyr) df1 %>%   group_by(v1) %>%    mutate(newcol = v3/v3[v2 == "ref"],           newcol = ifelse(!grepl("^s\\d+", v2), v3, newcol)) # tibble: 12 x 4 # groups:   v1 [2] #      v1    v2    v3 newcol #   <chr> <chr> <int>  <dbl> # 1       ref    20   20.0 # 2        s1    12    0.6 # 3        s2    76    3.8 # 4        s3    12    0.6 # 5        s4    12    0.6 # 6        xy    89   89.0 # 7     b   ref     2    2.0 # 8     b    s1    12    6.0 # 9     b    s2    42   21.0 #10     b    s3    21   10.5 #11     b    s4    12    6.0 #12     b    xy    56   56.0 

suppose, if need replace 'xy' values 'v3', replace last line newcol = ifelse(v2 == "xy", v3, newcol))

data

df1 <- structure(list(v1 = c("a", "a", "a", "a", "a", "a", "b", "b",  "b", "b", "b", "b"), v2 = c("ref", "s1", "s2", "s3", "s4", "xy",  "ref", "s1", "s2", "s3", "s4", "xy"), v3 = c(20l, 12l, 76l, 12l,  12l, 89l, 2l, 12l, 42l, 21l, 12l, 56l)), .names = c("v1", "v2",  "v3"), class = "data.frame", row.names = c(na, -12l)) 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -