[R] how to change automatically 0=no and 1=yes

Marc Schwartz MSchwartz at mn.rr.com
Wed Jun 15 16:16:04 CEST 2005


On Wed, 2005-06-15 at 15:58 +0200, Muhammad Subianto wrote:
> Dear R-helpers,
> I have dataset (data.frame) like below,
>    x1  x2  x3   x4   x5  x6  x7  x8  x9 ... x1200
>     0    0    0    1    1    0    0    1    1
>     1    0    0    1    1    0    0    1    1
>     0    1    0    1    1    0    0    1    1
>     1    1    0    1    1    0    0    1    1
> ...
> How can I change automatically 0=no and 1=yes.
> 
> Thank you very much in advance.
> Kindly regards,
> Muhammad Subianto

The easiest might be to use the following, presuming that your data
frame is called 'df' and all entries are 0/1:

> df
   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1   1  0  1  1  1  1  0  1  1   0
2   1  0  1  0  0  0  1  1  0   0
3   0  0  1  0  1  1  1  1  1   0
4   0  0  0  0  1  1  0  1  0   0
5   1  1  1  1  0  1  0  1  1   0
6   1  0  1  1  1  1  0  1  1   1
7   0  1  1  1  0  0  1  0  1   0
8   1  1  1  1  0  0  1  1  0   0
9   1  0  1  1  1  0  1  0  1   0
10  1  0  0  1  1  1  1  1  0   1

# Use ifelse(). By default that will return a
# character matrix, so coerce back to a data
# frame. Note that the entries are "factors"

> as.data.frame(ifelse(df == 0, "No", "Yes"))
    V1  V2  V3  V4  V5  V6  V7  V8  V9 V10
1  Yes  No Yes Yes Yes Yes  No Yes Yes  No
2  Yes  No Yes  No  No  No Yes Yes  No  No
3   No  No Yes  No Yes Yes Yes Yes Yes  No
4   No  No  No  No Yes Yes  No Yes  No  No
5  Yes Yes Yes Yes  No Yes  No Yes Yes  No
6  Yes  No Yes Yes Yes Yes  No Yes Yes Yes
7   No Yes Yes Yes  No  No Yes  No Yes  No
8  Yes Yes Yes Yes  No  No Yes Yes  No  No
9  Yes  No Yes Yes Yes  No Yes  No Yes  No
10 Yes  No  No Yes Yes Yes Yes Yes  No Yes

See ?ifelse for more information.

HTH,

Marc Schwartz




More information about the R-help mailing list