adding new level with new values and transpose the dataframe

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

adding new level with new values and transpose the dataframe

Temel İspanyolca
* Record Date*

*Classification Description*

*Current Month Budget Amount*



2020-06-30

Total Surplus (+) or Deficit (-)

 -864,074,068,492



2020-06-30

Borrowing from the Public

  716,272,172,707



2020-06-30

By Other Means

  420,704,500,379



2020-05-31

Borrowing from the Public

  759,987,728,236



2020-05-31

By Other Means

  -92,071,767,771



2020-05-31

Total Surplus (+) or Deficit (-)

 -398,821,413,660

Dear

I have a 2 questions

I have 3 different factors for every month in my dataframe.The column
of classification description has 3 factors.

Question 1:
I need to add a new factor to the column of classification description but
this *will be a summation* of other 3 factors in every month. How can I
realize this?

Question 2:
How can I transpose my dataframe as the following ?



Total Surplus (+) or Deficit (-)

Borrowing from the Public

By Other Means

2020-06-30

-864,074,068,492





716,272,172,707



420,704,500,379

2020-05-31

  759,987,728,236





-92,071,767,771



-398,821,413,660

Sincerely
Engin YILMAZ

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: adding new level with new values and transpose the dataframe

Jim Lemon-4
Hi Engin,
If you know what all the levels are, you can specify these in the
format command:

Classification_Description<-factor(Classification_Description,
 levels=c("Total Surplus (+) or Deficit (-)","Borrowing from the Public",
  "By other means"))

This ensures that all the levels are encoded even if one or more
levels is missing in the incoming data.

Jim

On Thu, Jul 30, 2020 at 8:33 PM Engin Yılmaz <[hidden email]> wrote:

>
> * Record Date*
>
> *Classification Description*
>
> *Current Month Budget Amount*
>
>
>
> 2020-06-30
>
> Total Surplus (+) or Deficit (-)
>
>  -864,074,068,492
>
>
>
> 2020-06-30
>
> Borrowing from the Public
>
>   716,272,172,707
>
>
>
> 2020-06-30
>
> By Other Means
>
>   420,704,500,379
>
>
>
> 2020-05-31
>
> Borrowing from the Public
>
>   759,987,728,236
>
>
>
> 2020-05-31
>
> By Other Means
>
>   -92,071,767,771
>
>
>
> 2020-05-31
>
> Total Surplus (+) or Deficit (-)
>
>  -398,821,413,660
>
> Dear
>
> I have a 2 questions
>
> I have 3 different factors for every month in my dataframe.The column
> of classification description has 3 factors.
>
> Question 1:
> I need to add a new factor to the column of classification description but
> this *will be a summation* of other 3 factors in every month. How can I
> realize this?
>
> Question 2:
> How can I transpose my dataframe as the following ?
>
>
>
> Total Surplus (+) or Deficit (-)
>
> Borrowing from the Public
>
> By Other Means
>
> 2020-06-30
>
> -864,074,068,492
>
>
>
>
>
> 716,272,172,707
>
>
>
> 420,704,500,379
>
> 2020-05-31
>
>   759,987,728,236
>
>
>
>
>
> -92,071,767,771
>
>
>
> -398,821,413,660
>
> Sincerely
> Engin YILMAZ
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: adding new level with new values and transpose the dataframe

Rasmus Liland-3
Hi Engin,

On 2020-07-30 21:13 +1000, Jim Lemon wrote:
| On Thu, Jul 30, 2020 at 8:33 PM Engin Yılmaz wrote:
| |
| | I have 3 different factors for every
| | month in my dataframe.The column of
| | classification description has 3
| | factors.
| |
| | Question 1:
| | I need to add a new factor to the
| | column of classification description
| | but this *will be a summation* of
| | other 3 factors in every month. How
| | can I realize this?
|
| Hi Engin,
| If you know what all the levels are,
| you can specify these in the format
| command:
|
| Classification_Description<-factor(Classification_Description,
|  levels=c("Total Surplus (+) or Deficit (-)","Borrowing from the Public",
|   "By other means"))
|
| This ensures that all the levels are
| encoded even if one or more levels is
| missing in the incoming data.

... and you can add a new/change
the order of the levels later on
using levels() like this:

        levels(engin[,"Classification Description"]) <-
          c(levels(engin[,"Classification Description"]),
            "New level")

| | Question 2:
| | How can I transpose my dataframe as
| | the following ?

This is the same as the first example in
?reshape on Indometh long to wide.

        reshape(data=engin,
          v.names="Current Month Budget Amount",
          idvar="Record Date",
          timevar="Classification Description",
          direction="wide")

P.S. dput() is such a great function for
pasting data in emails like this:

        engin <- structure(list(
        `Record Date` = c("2020-06-30",
        "2020-06-30", "2020-06-30",
        "2020-05-31", "2020-05-31",
        "2020-05-31"),
        `Classification Description` =
        structure(c(3L, 1L, 2L, 1L, 2L, 3L),
        .Label = c("Borrowing from the Public",
        "By Other Means",
        "Total Surplus (+) or Deficit (-)"),
        class = "factor"),
        `Current Month Budget Amount` =
        c("-864,074,068,492", "716,272,172,707",
        "420,704,500,379", "759,987,728,236",
        "-92,071,767,771", "-398,821,413,660")),
        row.names = c(NA, -6L),
        class = "data.frame")

Rasmus

______________________________________________
[hidden email] 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.

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: adding new level with new values and transpose the dataframe

Rasmus Liland-3
On 2020-07-30 18:07 +0300, Engin Yılmaz wrote:
| El jueves, 30 de julio de 2020, Rasmus Liland escribió:
| | On 2020-07-30 21:13 +1000, Jim Lemon wrote:
| | | On Thu, Jul 30, 2020 at 8:33 PM Engin Yılmaz wrote:
| | | |
| | | | I have 3 different factors for every
| | | | month in my dataframe.The column of
| | | | classification description has 3
| | | | factors.
| | | |
| | | | Question 1:
| | | | I need to add a new factor to the
| | | | column of classification description
| | | | but this *will be a summation* of
| | | | other 3 factors in every month. How
| | | | can I realize this?
| | |
| | | Hi Engin,
| | | If you know what all the levels are,
| | | you can specify these in the format
| | | command:
| | |
| | | Classification_Description<-factor(Classification_Description,
| | |  levels=c("Total Surplus (+) or Deficit (-)","Borrowing from the Public",
| | |   "By other means"))
| | |
| | | This ensures that all the levels are
| | | encoded even if one or more levels is
| | | missing in the incoming data.
| |
| | ... and you can add a new/change
| | the order of the levels later on
| | using levels() like this:
| |
| |         levels(engin[,"Classification Description"]) <-
| |           c(levels(engin[,"Classification Description"]),
| |             "New level")
| |
| | | | Question 2:
| | | | How can I transpose my dataframe as
| | | | the following ?
| |
| | This is the same as the first example in
| | ?reshape on Indometh long to wide.
| |
| |         reshape(data=engin,
| |           v.names="Current Month Budget Amount",
| |           idvar="Record Date",
| |           timevar="Classification Description",
| |           direction="wide")
|
| Dear liland
|
| 1-new level *will be a summation* of
| other 3 factors in every month?

Oh, you mean setting a group of levels
to the same level, right?  Well, you
could select them using match, like in
the other mail thread:

        idx <-
          levels(engin[,"Classification Description"]) %in%
            c("Borrowing from the Public",
              "Some other outrageous source of income")
        levels(engin[,"Classification Description"])[idx] <-
          "By Other Means"

Got some errors, thus did some
“reading”[1], perhaps it is useful in
this case to use reshape2 instead of the
regular reshape to do the summation:

        my_func <- function(x) {
          paste0(deparse(x), collapse="")
        }
        engin[,"Current Month Budget Amount"] <-
          as.numeric(gsub(",", "",
            engin[,"Current Month Budget Amount"]))
        engin.melt <- reshape2::melt(engin,
          id.vars=
            c("Record Date",
              "Classification Description"))
        colnames(engin.melt)[1:2] <-
          c("date", "class")
        reshape2::dcast(
          data=engin.melt,
          formula=date~class+.,
          value.var="value",
          fun.aggregate=my_func)
        reshape2::dcast(
          data=engin.melt,
          formula=date~class+.,
          value.var="value",
          fun.aggregate=sum)

| 2-For your transpose approach, I have
| a 200 rows

200 “Record Date” rows or?  Please
explain.

Best,
Rasmus

[1] https://stackoverflow.com/questions/20795290/why-is-it-returning-a-warning-when-reshaping-in-r

P.S. adding this into the list again ...

______________________________________________
[hidden email] 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.

signature.asc (849 bytes) Download Attachment