[R] Replace the value with 1 and 0

Clint Bowman clint at ecy.wa.gov
Thu Feb 26 00:40:44 CET 2015


or:

with(yourData,table(year,month,yourData[["rain"]]>0))

Clint Bowman			INTERNET:	clint at ecy.wa.gov
Air Quality Modeler		INTERNET:	clint at math.utah.edu
Department of Ecology		VOICE:		(360) 407-6815
PO Box 47600			FAX:		(360) 407-7534
Olympia, WA 98504-7600

         USPS:           PO Box 47600, Olympia, WA 98504-7600
         Parcels:        300 Desmond Drive, Lacey, WA 98503-1274

On Thu, 26 Feb 2015, Peter Alspach wrote:

> Tena koe
>
> Something like:
>
> set.seed(153)
> # Create some (unrealistic) rainfall data
> yourData <- data.frame(year=rep(1950:1954, each=10), month=rep(rep(1:2, each=5), 5), rain=sample(0:1, 50, replace=TRUE)*round(rnorm(50, 20, 2), 1))
> tapply(yourData$rain>0, yourData[,c('year','month')], sum)
>
> will give your final table, but it will need some tidying up.
>
> HTH ....
>
> Peter Alspach
>
>
> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of smart hendsome
> Sent: Thursday, 26 February 2015 11:54 a.m.
> To: r-help at r-project.org
> Subject: [R] Replace the value with 1 and 0
>
> Hi everyone,
> I have this kind of rainfall dataset:
>    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
>
> I want to set as rain for Amount> 0 and not rain for Amount = 0.  I want to replace the Amount>0 with 1 and Amount equal to zero with 0.  Then I want to count how many rain in that particular month in that year. Anyone can help me?
> This is what I want:
> Year Month Day Amount
> 1  1950     1   1     0
> 2  1950     1   2     1
> 3  1950     1   3     1
> 4  1950     1   4     1
> 5  1950     1   5     1
> 6  1950     1   6     1
> 7  1950     1   7     1
> 8  1950     1   8     1
> 9  1950     1   9     1
> 10 1950     1  10    1
> 11 1950     1  11     1
> 12 1950     1  12    1
> 13 1950     1  13    1
> 14 1950     1  14    1
> 15 1950     1  15    1
> 16 1950     1  16    1
> 17 1950     1  17    1
> 18 1950     1  18    1
> 19 1950     1  19    1
> 20 1950     1  20    1
> Then become like this:
>
> |
> | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec |
> | 1950 | 17 | 6 | 23 | 20 | 19 | 9 | 17 | 23 | 18 | 20 | 20 | 17 |
> | 1951 | 23 | 19 | 20 | 20 | 19 | 11 | 16 | 20 | 22 | 25 | 25 | 16 |
> | 1952 | 15 | 21 | 30 | 24 | 23 | 20 | 16 | 19 | 20 | 19 | 21 | 15 |
>
>
> Thanks.
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
> The contents of this e-mail are confidential and may be subject to legal privilege.
> If you are not the intended recipient you must not use, disseminate, distribute or
> reproduce all or any part of this e-mail or attachments.  If you have received this
> e-mail in error, please notify the sender and delete all material pertaining to this
> e-mail.  Any opinion or views expressed in this e-mail are those of the individual
> sender and may not represent those of The New Zealand Institute for Plant and
> Food Research Limited.
> ______________________________________________
> 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