Hi,
I am trying to create a function that adds the total value corresponding to a factor within a data frame, as a column (I use sumif for it in excel)
This is my data table
u_value<-seq(23,34,length=10)
u_name<-c("a","z","b","j","k","u","a","z","b","a")
tablo45<-data.frame(u_name,u_value)
I want the output to be like this
When I use this commands one by one, I obtain the desired output
ara<-aggregate(u_value ~ u_name, data = tablo45, sum)
df1<-merge(cbind(tablo45), cbind(ara),by.x = "u_name",by.y="u_name",no.dups = FALSE)
But when i try to add this as a function, it doesn't work
ekle_etopla<-function(df,a,b) {
ara<-aggregate(b ~ a, data = df, sum)
df1<-merge(cbind(df), cbind(ara),by.x = "a",by.y="a",no.dups = FALSE)
df1}
this is the error message:
"Error in fix.by(by.x, x) : 'by' must specify a uniquely valid column"
Can someone help me with this? I tried so many variations but I am stuck.
Thanks