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

From: Marc Schwartz <MSchwartz_at_mn.rr.com>
Date: Thu 16 Jun 2005 - 00:16:04 EST

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



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Jun 16 00:24:11 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:42 EST