grep()
returns a vector of indices of entries that match the given criteria.
The only way that your code could work here is if the number of rows of data
equals some even multiple of the number of matches grep()
finds.
Consider the following reproducible example:
data = data.frame(RAW_MATERIAL_DIMENSION = c("BAC","bBAC","aBAC","BACK","lbd"))
> data
RAW_MATERIAL_DIMENSION
1 BAC
2 bBAC
3 aBAC
4 BACK
5 lbd
> grep("^BAC",data$RAW_MATERIAL_DIMENSION)
[1] 1 4
data$CleanDim <- data$RAW_MATERIAL_DIMENSION[grep("^BAC",data$RAW_MATERIAL_DIMENSION)]
Error in `$<-.data.frame`(`*tmp*`, CleanDim, value = 1:2) :
replacement has 2 rows, data has 5
Note: this would work out ok (though it would be pretty weird) if the original data
object just had its first four rows. In that case, you'd just get repeated values populated in your new column.
But, what you want to do here is to look at the results of grep("^BAC",data$RAW_MATERIAL_DIMENSION)
and think about what is going to be sensible in your context. Your operation will only work if the length of this result equals that of your data
object, or at least if your data object is a whole multiple of that length.