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]]
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.
Hi, Here is an implementation. More data are added. An extra column hasRain is added instead of replacing column Amount.> rainYear 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) > rainYear 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.
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. >
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. >