簡體   English   中英

R:按列組匯總數據-使用每個觀察值對列進行變異

[英]R: Aggregating data by column group - mutate column with values for each observation

我遇到一個初學者的問題,即匯總一個類別的數據的數據,創建一個新列,其中包含每個遵守情況下每個類別的數據的總和。

我想要以下數據:

PIN Balance
221 5000
221 2000
221 1000
554 4000
554 4500
643 6000
643 4000

看起來像:

PIN Balance Total
221 5000 8000
221 2000 8000
221 1000 8000
554 4000 8500
554 4500 8500
643 6000 10000
643 4000 10000

我試過使用聚合:輸出<-聚合(df $ Balance〜df $ PIN,數據= df,總和),但是由於關閉的次數太少,因此無法將數據返回到我的原始數據集中。

您可以使用dplyr執行您想要的操作。 我們首先group_by PIN ,然后創建一個新列Total使用mutate是分組的總和Balance

library(dplyr)
res <- df %>% group_by(PIN) %>% mutate(Total=sum(Balance))

將數據用作數據框df

df <- structure(list(PIN = c(221L, 221L, 221L, 554L, 554L, 643L, 643L
), Balance = c(5000L, 2000L, 1000L, 4000L, 4500L, 6000L, 4000L
)), .Names = c("PIN", "Balance"), class = "data.frame", row.names = c(NA, 
-7L))
##  PIN Balance
##1 221    5000
##2 221    2000
##3 221    1000
##4 554    4000
##5 554    4500
##6 643    6000
##7 643    4000

我們得到了預期的結果:

print(res)
##Source: local data frame [7 x 3]
##Groups: PIN [3]
##
##    PIN Balance Total
##  <int>   <int> <int>
##1   221    5000  8000
##2   221    2000  8000
##3   221    1000  8000
##4   554    4000  8500
##5   554    4500  8500
##6   643    6000 10000
##7   643    4000 10000

或者我們可以使用data.table

library(data.table)
setDT(df)[,Table:=sum(Balance),by=PIN][]
##    PIN Balance Total
##1:  221    5000  8000
##2:  221    2000  8000
##3:  221    1000  8000
##4:  554    4000  8500
##5:  554    4500  8500
##6:  643    6000 10000
##7:  643    4000 10000

考慮使用sapply()條件和方法的基本R解決方案:

df <- read.table(text="PIN Balance
                 221 5000
                 221 2000
                 221 1000
                 554 4000
                 554 4500
                 643 6000
                 643 4000", header=TRUE)    

df$Total <- sapply(seq(nrow(df)), function(i){
  sum(df[df$PIN == df$PIN[i], c("Balance")])
}) 

#   PIN Balance Total
# 1 221    5000  8000
# 2 221    2000  8000
# 3 221    1000  8000
# 4 554    4000  8500
# 5 554    4500  8500
# 6 643    6000 10000
# 7 643    4000 10000

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM