I have a df
that are in different unit. I would like to convert them into the same unit based on the conversion factors in cov
. When df$Test==cov$Type, df$Unit==cov$Raw, then we do the calculation. if df$unit can not be found in cov, then keep as is and flag it with new variable "Check"=="Y"
What will be the best way to complete this conversion process. I saw someone using the method: building an empty df then read in each record one by one with calculation. Is it a good way? or it is the way counts as more careful way? I would like to know what will you do if you are the person to handle such task. As many as possible. Many thanks.
df<-structure(list(Test = c("Length", "Weight", "Weight", "Weight",
"Weight", "Length", "Length", "Length", "Length", "Length", "Length",
"Length", "Length", "Length"), Result = c(4.5, 36, 147, 55, 175,
2, 125, 222, 1.6, 3, 56, 512, 28, 78), Unit = c("m", "lb", "g",
"kg", "oz", "cm", "in", "mm", "ft", "m", "in", "cm", "cm", NA
)), row.names = c(NA, -14L), class = c("tbl_df", "tbl", "data.frame"))
cov<- structure(list(Type = c("Length", "Length", "Length", "Length",
"Length", "Weight", "Weight", "Weight", "Weight"), Raw = c("m",
"cm", "mm", "in", "ft", "lb", "g", "kg", "oz"), Standard = c("cm",
"cm", "cm", "cm", "cm", "g", "g", "g", "g"), Factor = c(100,
1, 0.1, 2.54, 30.48, 453, 1, 1000, 28)), row.names = c(NA, -9L
), class = c("tbl_df", "tbl", "data.frame"))