[R] Replace the value with 1 and 0

Göran Broström goran.brostrom at umu.se
Thu Feb 26 09:59:03 CET 2015



On 2015-02-26 00:33, JS Huang wrote:
> Hi,
>
>    Here is an implementation.  More data are added.  An extra column hasRain
> is added instead of replacing column Amount.
>
>> rain
>     Year Month Day Amount
> 1  1950     1   1    0.0
> 2  1950     1   2   35.5
> 3  1950     1   3   17.8
> 4  1950     1   4   24.5
> 5  1950     1   5   12.3
> 6  1950     1   6   11.5
> 7  1950     1   7    5.7
> 8  1950     1   8   13.2
> 9  1950     1   9   11.3
> 10 1950     1  10   14.7
> 11 1950     1  11   11.9
> 12 1950     1  12   17.5
> 13 1950     1  13    8.1
> 14 1950     1  14    0.4
> 15 1950     1  15    0.0
> 16 1950     1  16   19.5
> 17 1950     1  17   10.7
> 18 1950     1  18    0.5
> 19 1950     1  19   12.7
> 20 1950     1  20    6.3
> 21 1950     2   1    0.0
> 22 1950     2   2   35.5
> 23 1950     2   3   17.8
> 24 1950     2   4   24.5
> 25 1950     2   5   12.3
> 26 1950     2   6   11.5
> 27 1950     2   7    5.7
> 28 1950     2   8   13.2
> 29 1950     2   9   11.3
> 30 1950     2  10   14.7
> 31 1950     2  11   11.9
> 32 1950     2  12   17.5
> 33 1950     2  13    8.1
> 34 1950     2  14    0.4
> 35 1950     2  15    0.0
> 36 1950     2  16   19.5
> 37 1950     2  17   10.7
> 38 1950     2  18    0.0
> 39 1950     2  19    0.0
> 40 1950     2  20    0.0
>> rain$hasRain <- ifelse(rain$Amount>0,1,0)

No! Better is

rain$hasRain <- as.numeric(rain$Amount > 0)

See previous discussions about the use of 'ifelse'.

Göran

>> rain
>     Year Month Day Amount hasRain
> 1  1950     1   1    0.0       0
> 2  1950     1   2   35.5       1
> 3  1950     1   3   17.8       1
> 4  1950     1   4   24.5       1
> 5  1950     1   5   12.3       1
> 6  1950     1   6   11.5       1
> 7  1950     1   7    5.7       1
> 8  1950     1   8   13.2       1
> 9  1950     1   9   11.3       1
> 10 1950     1  10   14.7       1
> 11 1950     1  11   11.9       1
> 12 1950     1  12   17.5       1
> 13 1950     1  13    8.1       1
> 14 1950     1  14    0.4       1
> 15 1950     1  15    0.0       0
> 16 1950     1  16   19.5       1
> 17 1950     1  17   10.7       1
> 18 1950     1  18    0.5       1
> 19 1950     1  19   12.7       1
> 20 1950     1  20    6.3       1
> 21 1950     2   1    0.0       0
> 22 1950     2   2   35.5       1
> 23 1950     2   3   17.8       1
> 24 1950     2   4   24.5       1
> 25 1950     2   5   12.3       1
> 26 1950     2   6   11.5       1
> 27 1950     2   7    5.7       1
> 28 1950     2   8   13.2       1
> 29 1950     2   9   11.3       1
> 30 1950     2  10   14.7       1
> 31 1950     2  11   11.9       1
> 32 1950     2  12   17.5       1
> 33 1950     2  13    8.1       1
> 34 1950     2  14    0.4       1
> 35 1950     2  15    0.0       0
> 36 1950     2  16   19.5       1
> 37 1950     2  17   10.7       1
> 38 1950     2  18    0.0       0
> 39 1950     2  19    0.0       0
> 40 1950     2  20    0.0       0
>> tapply(rain$hasRain,list(rain$Year,rain$Month),sum)
>        1  2
> 1950 18 15
>>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Replace-the-value-with-1-and-0-tp4703838p4703840.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list