I have picked up an awful public data set that needs a lot of work to make it useful. Here is a simplification:
Molten<-data.frame(ID=round(runif(100, 0, 50),0), Element=c(rep("Au", 20), rep("Fe", 10),
rep("Al", 30),rep("Cu", 20),rep("Au", 20)),
Measure=rnorm(100), Units=c(rep("ppm",10), rep("pct",10), rep("ppb", 80)))
Molten$UnitElement<-paste(Molten$Element, Molten$Units, sep="_")
Molten<-Molten[!duplicated(Molten[,c("ID", "Element")]),]
I have arrived at a data frame with the IDs and a different column for each element using dcast:
library(reshape2)
Cast<-dcast(Molten, ID~Element, value.var="Measure" )
But there are different units of measure for the same element. So I will need an extra column for each element indicating what unit that record is measured in. For example a column called "GoldUnit" with NA for each entry without a gold measurement and the measured unit for each populated gold record. I'm not sure how to go about this. Any help would be appreciated!
Example of what I would like
ID, Al, Al_unit, Au, Au_unit, Cu, Cu_unit, Fe, Fe_unit
5, NA, NA, NA, NA, 1, "ppb", NA, NA
7, NA, NA, NA, NA, NA , NA, 6, "ppb"
3, 3, "ppb", 4, "ppm", NA, NA, NA, NA
This should return what you're looking for:
Try
If you need to change the column names