You can copy the following code to an R
script file and run it:
preprocess_brand_version = function(dataset) {
dataset$brand_version = gsub("^([0-9]+)(\\.[0-9]+)?.*$", "\\1\\2", dataset$brand_version, perl = TRUE)
dataset = dataset %>% mutate(
brand_version = ifelse(!(is.na(brand) || is.na(brand_version)), paste(substr(brand, 1, 3), ", ", brand_version, sep = ""), NA)
)
dataset$brand_version = as.factor(dataset$brand_version)
return (dataset)
}
a = data.frame(brand = c("Samsung", "Motorola"), brand_version = c("1.4.3", "6.3"))
b = a
b[1,2] = NA
a
b
preprocess_brand_version(b)
My problem is that when I run that, I get:
> a
brand brand_version
1 Samsung 1.4.3
2 Motorola 6.3
> b
brand brand_version
1 Samsung <NA>
2 Motorola 6.3
> preprocess_brand_version(b)
brand brand_version
1 Samsung <NA>
2 Motorola <NA>
I was expecting to get: "Mot, 6.3" as the new value for the version on Motorola row.
Any idea why the: if_else
is not working as I would expect?
Thanks!