I have a data frame like this one:
dat1 = data.frame("name" = c("Peter", "Tom", "Peter", "Peter", "Tom", "Tom"), "adress" = c("str1", "str2", "str1", "str2", "str3", "str3"), "product" = c("prod1", "prod1", "prod2", "prod3", "prod2", "prod2"), "val" = c(1,2,3,4,5,5))
Now I would like to create a new data frame which has columns "name", "adress" and all unique values of "product". The values in the columns of type "product" should be the sums of "val". Here´s the desired output:
result = data.frame("name" = c("Peter", "Tom", "Peter", "Tom"), "adress" = c("str1", "str2", "str2", "str3"), "prod1" = c(1,NA,NA,NA), "prod2" = c(3,NA,NA,10), "prod3" = c(NA,NA,4,NA))
Any ideas?