[R] adding new level with new values and transpose the dataframe
Rasmus Liland
jr@| @end|ng |rom po@teo@no
Thu Jul 30 15:03:35 CEST 2020
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20200730/52b7a1ef/attachment.sig>
More information about the R-help
mailing list