[R] How to combine two rows in a data table into a third new row, such that the values in the row are added together in the new row?

Rasmus Liland jr@| @end|ng |rom po@teo@no
Thu May 7 00:35:15 CEST 2020


On 2020-05-06 09:13 +0000, PIKAL Petr wrote:
> Maybe aggregate?

Hi!  I agree aggregate is an elegant solution 
for this, so I continued your example a bit:

dt_count <- '"","STATUS","N"
"1","Resolved",650
"2","Assigned",135
"3","Closed",530
"4","In Progress",56
"5","Pending",75
"6","Cancelled",20'
dt_count <- read.csv(text=dt_count)

dt_count[
  dt_count$STATUS %in%
  c("Resolved", "Closed"),
  "STATUS"] <-
  "Resolved/Closed"
aggregate(
  x=list("N"=dt_count$N),
  by=list("STATUS"=dt_count$STATUS),
  FUN=sum)

Best,
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/20200507/5178fe64/attachment.sig>


More information about the R-help mailing list