我有以下數據集,我想確定每個customer_ID具有最高金額的產品並將其轉換爲新列。我也想每個ID只保留一條記錄。查找每個分組變量的最大值並將其轉換爲新變量
數據生成數據集:
x <- data.frame(customer_id=c(1,1,1,2,2,2), product=c("a","b","c","a","b","c"), amount=c(50,125,100,75,110,150))
實際數據集是這樣的:
customer_id product amount 1 a 50 1 b 125 1 c 100 2 a 75 2 b 110 2 c 150
所需的輸出想應該是這樣的:
customer_ID product_b product_c 1 125 0 2 0 150