[R] Extract data

Jim Lemon drjimlemon at gmail.com
Fri Jul 29 10:52:31 CEST 2016


Hi Roslina,
This may be what you want:

sum_balok<-
 as.vector(by(aggbalok_2009_2014$x,aggbalok_2009_2014$year,sum,na.rm=TRUE))
cbind(year=2009:2014,sum_balok)

I don't have the data for the other measures, but you could calculate
the sums as you did below and then add them to the cbind arguments.

Jim


On Fri, Jul 29, 2016 at 5:51 PM, roslinazairimah zakaria
<roslinaump at gmail.com> wrote:
> I tried this:
> dt1 <- aggbalok_mth[aggbalok_mth$year %in% 2009:2014,]
> dt2 <- agggambang_mth[agggambang_mth$year %in% 2009:2014,]
> dt3 <- aggsgsoi_mth[aggsgsoi_mth$year %in% 2009:2014,]
> dt4 <- aggjpsphg_mth[aggjpsphg_mth$year %in% 2009:2014,]
> dt5 <- aggpbesar_mth[aggpbesar_mth$year %in% 2009:2014,]
> dt6 <- aggpmanis_mth[aggpmanis_mth$year %in% 2009:2014,]
>
> ## Yearly Sum
> dt_year <- cbind(aggregate(dt1[,3], by=dt1[,c(2,2)],FUN=sum, na.rm=TRUE),
>       aggregate(dt2[,3], by=dt2[,c(2,2)],FUN=sum, na.rm=TRUE),
>       aggregate(dt3[,3], by=dt3[,c(2,2)],FUN=sum, na.rm=TRUE),
>       aggregate(dt4[,3], by=dt4[,c(2,2)],FUN=sum, na.rm=TRUE),
>       aggregate(dt5[,3], by=dt5[,c(2,2)],FUN=sum, na.rm=TRUE),
>       aggregate(dt6[,3], by=dt6[,c(2,2)],FUN=sum, na.rm=TRUE))
>
> However the output is not good.
> dput(dt_year)
> structure(list(year = c(2009, 2010, 2011, 2012, 2013, 2014),
>     year.1 = c(2009, 2010, 2011, 2012, 2013, 2014), x = c(3511.6,
>     2456, 3575.7, 3029, 2849.7, 1697.7), year = c(2009, 2010,
>     2011, 2012, 2013, 2014), year.1 = c(2009, 2010, 2011, 2012,
>     2013, 2014), x = c(3040.3, 2209.1, 3210.3, 3403.8, 3449.3,
>     2070.4), year = c(2009, 2010, 2011, 2012, 2013, 2014), year.1 = c(2009,
>     2010, 2011, 2012, 2013, 2014), x = c(3657.1, 2059.2, 2862.4,
>     3221.2, 3136.8, 3255.1), year = c(2009, 2010, 2011, 2012,
>     2013, 2014), year.1 = c(2009, 2010, 2011, 2012, 2013, 2014
>     ), x = c(2726.4, 2266.8, 2249.1, 3093.5, 1513, 3087.5), year = c(2009,
>     2010, 2011, 2012, 2013, 2014), year.1 = c(2009, 2010, 2011,
>     2012, 2013, 2014), x = c(3194.9, 1879.6, 1861.4, 3080, 1648.4,
>     3012.3), year = c(2009, 2010, 2011, 2012, 2013, 2014), year.1 = c(2009,
>     2010, 2011, 2012, 2013, 2014), x = c(2697.9, 1639.7, 2107.8,
>     2479.7, 3078.8, 2288.7)), .Names = c("year", "year.1", "x",
> "year", "year.1", "x", "year", "year.1", "x", "year", "year.1",
> "x", "year", "year.1", "x", "year", "year.1", "x"), row.names = c(NA,
> -6L), class = "data.frame")
>
> Thank you.
>
> On Fri, Jul 29, 2016 at 2:30 PM, roslinazairimah zakaria
> <roslinaump at gmail.com> wrote:
>>
>> I have one more question, how do I get the sum for the years.  Thank you.
>>
>> On Fri, Jul 29, 2016 at 12:36 PM, roslinazairimah zakaria
>> <roslinaump at gmail.com> wrote:
>>>
>>> Thank you very much Jim.  It works beautifully.
>>>
>>> Best regards,
>>>
>>>
>>>
>>> On Fri, Jul 29, 2016 at 11:56 AM, Jim Lemon <drjimlemon at gmail.com> wrote:
>>>>
>>>> Hi Roslina,
>>>> Try this:
>>>>
>>>>  aggbalok_mth[aggbalok_mth$year %in% 2009:2014,]
>>>>
>>>> Jim
>>>>
>>>>
>>>> On Fri, Jul 29, 2016 at 1:12 PM, roslinazairimah zakaria
>>>> <roslinaump at gmail.com> wrote:
>>>> > Dear r-users,
>>>> >
>>>> > I would like to extract year from 2009 to 2014 with the corresponding
>>>> > month
>>>> > and rain amount.
>>>> >
>>>> > I tried this:
>>>> > aggbalok_mth[aggbalok_mth$year == 2009:2014, ]  but some of the data
>>>> > is
>>>> > missing.
>>>> >
>>>> >> dput(aggbalok_mth[aggbalok_mth$year == 2009:2014, ] )
>>>> > structure(list(month = c(1, 7, 2, 8, 3, 9, 4, 10, 5, 11, 6, 12
>>>> > ), year = c(2009, 2009, 2010, 2010, 2011, 2011, 2012, 2012, 2013,
>>>> > 2013, 2014, 2014), x = c(424.6, 59.5, 6, 54.6, 387.9, 236.1,
>>>> > 160.3, 162.5, 102.8, 139.5, 293.3, 39)), .Names = c("month",
>>>> > "year", "x"), row.names = c(7L, 13L, 20L, 26L, 33L, 39L, 46L,
>>>> > 52L, 59L, 65L, 72L, 78L), class = "data.frame")
>>>> > Warning message:
>>>> > In aggbalok_mth$year == 2009:2014 :
>>>> >   longer object length is not a multiple of shorter object length
>>>> >
>>>> >
>>>> > Here is the my data:
>>>> >
>>>> > structure(list(month = c(7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5,
>>>> > 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
>>>> > 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8,
>>>> > 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3,
>>>> > 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
>>>> > 11), year = c(2008, 2008, 2008, 2008, 2008, 2008, 2009, 2009,
>>>> > 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2010,
>>>> > 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010,
>>>> > 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011,
>>>> > 2011, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012,
>>>> > 2012, 2012, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013,
>>>> > 2013, 2013, 2013, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014,
>>>> > 2014, 2014, 2014, 2014, 2015, 2015, 2015, 2015, 2015, 2015, 2015,
>>>> > 2015, 2015, 2015, 2015), x = c(0, 168.7, 203, 149.3, 299.9, 570.7,
>>>> > 424.6, 52.6, 407.7, 210.3, 459.8, 249.2, 59.5, 310.4, 182.7,
>>>> > 433.3, 161, 560.5, 197.5, 6, 68.9, 170.4, 117, 271.2, 133.5,
>>>> > 54.6, 145.5, 122.5, 460.9, 708, 646.7, 58.8, 387.9, 42.9, 190.7,
>>>> > 133.3, 131.7, 158.4, 236.1, 412.9, 462.5, 713.8, 437.1, 140.1,
>>>> > 311.4, 160.3, 202.4, 58.8, 134.7, 120.4, 206.9, 162.5, 68.5,
>>>> > 1025.9, 229.5, 331, 9, 51.4, 102.8, 162.9, 157.2, 32.6, 103.9,
>>>> > 158.7, 139.5, 1371.2, 221.5, 6.1, 19.1, 11, 87.7, 293.3, 87.3,
>>>> > 184, 69.5, 231, 448.2, 39, 19.3, 3.9, 53.8, 141.9, 325, 53.5,
>>>> > 133.3, 321.1, 77.6, 156.5, 2.2)), .Names = c("month", "year",
>>>> > "x"), row.names = c(NA, -89L), class = "data.frame")
>>>> >
>>>> > Thank you very much for any help given.
>>>> > --
>>>> > *Dr. Roslinazairimah Binti Zakaria*
>>>> > *Tel: +609-5492370; Fax. No.+609-5492766*
>>>> >
>>>> > *Email: roslinazairimah at ump.edu.my <roslinazairimah at ump.edu.my>;
>>>> > roslinaump at gmail.com <roslinaump at gmail.com>*
>>>> > Deputy Dean (Academic & Student Affairs)
>>>> > Faculty of Industrial Sciences & Technology
>>>> > University Malaysia Pahang
>>>> > Lebuhraya Tun Razak, 26300 Gambang, Pahang, Malaysia
>>>> >
>>>> >         [[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.
>>>
>>>
>>>
>>>
>>> --
>>> Dr. Roslinazairimah Binti Zakaria
>>> Tel: +609-5492370; Fax. No.+609-5492766
>>> Email: roslinazairimah at ump.edu.my; roslinaump at gmail.com
>>> Deputy Dean (Academic & Student Affairs)
>>> Faculty of Industrial Sciences & Technology
>>> University Malaysia Pahang
>>> Lebuhraya Tun Razak, 26300 Gambang, Pahang, Malaysia
>>
>>
>>
>>
>> --
>> Dr. Roslinazairimah Binti Zakaria
>> Tel: +609-5492370; Fax. No.+609-5492766
>> Email: roslinazairimah at ump.edu.my; roslinaump at gmail.com
>> Deputy Dean (Academic & Student Affairs)
>> Faculty of Industrial Sciences & Technology
>> University Malaysia Pahang
>> Lebuhraya Tun Razak, 26300 Gambang, Pahang, Malaysia
>
>
>
>
> --
> Dr. Roslinazairimah Binti Zakaria
> Tel: +609-5492370; Fax. No.+609-5492766
> Email: roslinazairimah at ump.edu.my; roslinaump at gmail.com
> Deputy Dean (Academic & Student Affairs)
> Faculty of Industrial Sciences & Technology
> University Malaysia Pahang
> Lebuhraya Tun Razak, 26300 Gambang, Pahang, Malaysia



More information about the R-help mailing list