[英]In R, how to sum all other columns based on value of one column, without specifying column names?
我有 401 列。 第 401 列代表 PIN 代碼。 我想根據 pin 碼添加所有其他 400 列。 我可以在不必指定所有 400 個列名的情況下執行此操作嗎?
我們可以做一個group_by
sum
。 使用dplyr
,將“ summarise_all
”作為分組變量傳遞,並使用 summarise_all 應用sum
,獲得數據中剩余的所有其他列的總和
library(dplyr)
df1 %>%
group_by(PINCODE) %>%
summarise_all(sum)
或從base R
aggregate
aggregate(.~ PINCODE, df1, sum)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.