轉換的data.frame爲 'data.table'(setDT(df1)
),按'ID','Type'分組,如果是觀察數大於1,按照OP的帖子所示進行劃分,否則返回0.移除'Flag'列,然後根據'ID'和'Type'得到'Value1'和'Value2'的sum
並採取unique
library(data.table)
setDT(df1)[, Result := if(.N >1) round(100*Value1[1]/(Value2[1]+Value2[2]), 3)
else 0, .(ID, Type)]
df2 <- df1[, -3, with = FALSE]
df2[, (3:4) := lapply(.SD, sum), .SDcols = Value1:Value2, .(ID, Type)]
unique(df2)
# ID Type Value1 Value2 Result
#1: 1 Apple 29313 30835 0.003
#2: 1 Orange 2222 3423 0.000
#3: 2 Orange 24566 32234 0.000
或者,我們可以在一個單一的流使用tidyverse
。由 'ID', '類型' 分組後,創建 '結果' 欄,然後獲得價值」的sum
columns with
mutate_at , remove the 'Flag' and get the
distinct`行
library(dplyr)
df1 %>%
group_by(ID, Type) %>%
mutate(Result = round(100*if(n()==2) first(Value1)/(first(Value2)+last(Value2))
else 0, 3)) %>%
mutate_at(vars(matches('Value')), sum) %>%
select(-Flag) %>%
distinct
# A tibble: 3 x 5
# Groups: ID, Type [3]
# ID Type Value1 Value2 Result
# <int> <chr> <int> <int> <dbl>
#1 1 Apple 29313 30835 0.003
#2 1 Orange 2222 3423 0.000
#3 2 Orange 24566 32234 0.000
嘿,謝謝!但是第一行的值2應該是30835! – AB6