r - How to use Apply function in a column dependent case? -
i trying fill new column in df of 700k records , goes slow loop , therefore want use apply function. not familiar , below attempt doesn't work. please help
myfunc <- function(a,b,c,d) {if (a=="xyz" & b==11) {c=d}} dataf[,'target'] <- apply(dataf, 1, function(dataf) myfunc(dataf[,'col1'],dataf[,'col2'],dataf[,'target'],dataf[,'col3']))
adding more description -
what have:
a b c d x 2 p x 2 p x 2 p xyz 11 p xyz 11 p xyz 2 p y 2 p y 2 p y 2 p
what want achieve:
a b c d x 2 p x 2 p x 2 p xyz 11 p p xyz 11 p p xyz 2 p y 2 p y 2 p y 2 p
given op, guessing want this??
library(data.table) setdt(dataf)[a == "xyz" & b == 11, c := d]
output:
b d c 1: x 2 p na 2: x 2 p na 3: x 2 p na 4: xyz 11 p p 5: xyz 11 p p 6: xyz 2 p na 7: y 2 p na 8: y 2 p na 9: y 2 p na
i highly suggest reading tutorial of data.table super-fast , can used lot of different things. on site find more articles. read them all, need of , lot!!
Comments
Post a Comment