[R] adding new level with new values and transpose the dataframe

Rasmus Liland jr@| @end|ng |rom po@teo@no
Thu Jul 30 18:44:42 CEST 2020


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 ...

-------------- 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/8f2aa111/attachment.sig>


More information about the R-help mailing list