Creating a new column in R that looks at the data from two previous columns and assigns a Factor level? -


i have data set looks @ 2 parasites of snails want create new column states whether snails have no parasites "none", type 1 "type1",type 2 "type2" or both parasites "dual".

   block weight parasite1  parasite2 1      1   1.23        1           1          2      1   3.14        1           1          3      1   2.55        1           0          4      1   2.67        0           1          5      1   3.36        0           1          6      1   3.16        0           0          7      1   3.41        0           1          8      1   2.47        0           1          9      1   1.56        0           1          10     1   2.66        1           1 

i have thought of using if or if else can't seem work?

you few if else statements, easier think

dat <- read.table(header = true, text="row block weight parasite1  parasite2 1      1   1.23        1           1          2      1   3.14        1           1          3      1   2.55        1           0          4      1   2.67        0           1          5      1   3.36        0           1          6      1   3.16        0           0          7      1   3.41        0           1          8      1   2.47        0           1          9      1   1.56        0           1          10     1   2.66        1           1")[,-1]   within(dat, {   type <- interaction(parasite1, parasite2, sep = '')   type <- factor(type, levels = c('11','10','01','00'),                  labels = c('dual','type1','type2','none')) })  #    block weight parasite1 parasite2  type # 1      1   1.23         1         1  dual # 2      1   3.14         1         1  dual # 3      1   2.55         1         0 type1 # 4      1   2.67         0         1 type2 # 5      1   3.36         0         1 type2 # 6      1   3.16         0         0  none # 7      1   3.41         0         1 type2 # 8      1   2.47         0         1 type2 # 9      1   1.56         0         1 type2 # 10     1   2.66         1         1  dual 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -