r - replace() function recodes entire column -


i using r, , need recode variables in list. list follows:

> list1  group.1     x 1        4000     2        3890 

where in group.1 column replace instance of 1 male, , 2 female.

i have tried replace() function:

replace(list1,list1$group.1 == "1", "male") 

which returns

group.1     x male        4000     male        3890 

i.e., has relplaced group.1 == 2 male. have missed step? far can see use of replace function - group.1 == 1 should limit replace command values in group.1 equal 1. seems criteria not doing anything.

replace operates on vectors, not data.frames. try this:

> replace(df$group.1,df$group.1 == 1, "male") [1] "male" "2"    

and change data, can do:

> df$group.1 <- replace(df$group.1,df$group.1 == 1, "male") > df   group.1    x 1    male 4000 2       2 3890 

what trying looks application factor column levels c("male", "female").


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -