Engin Yılmaz
2020-Jul-30 10:27 UTC
[R] adding new level with new values and transpose the dataframe
* 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]]
Jim Lemon
2020-Jul-30 11:13 UTC
[R] adding new level with new values and transpose the dataframe
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 <ispanyolcom at gmail.com> 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]] > > ______________________________________________ > 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.
Rasmus Liland
2020-Jul-30 13:03 UTC
[R] adding new level with new values and transpose the dataframe
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>
Rasmus Liland
2020-Jul-30 16:44 UTC
[R] adding new level with new values and transpose the dataframe
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>