I have a data frame "df" and want to apply if/else conditions to insert a decimal for the entire column "A"
A B
E0505 123
890 43
4505 56
Rules to apply:
Final output:
A B
E050.5 123
890 43
450.5 56
I have tried this, but I am not sure how to include the condition where row starts with E or not.
ifelse(str_length(df$A)>3, as.character(paste0(substring(df$A, 1, 3),".", substring(df$A, 4))), as.character(df$A))
Use sub with regular expression, you can do this:
df$A <- sub("((?:^E.|^[^E]).{2})(.+)", "\\1.\\2", df$A)
df
# A B
#1 E050.5 123
#2 890 43
#3 450.5 56
((?:^E.|^[^E]).{2})(.+) matches strings:
E followed by 4 or more characters, in which case capture the first 4 characters and the rest as two separate groups and insert . between;E but have 4 or more characters, in which case capture the first 3 characters and the rest as two separate groups and insert . between;Strings starting with E and has less than 5 characters in total or not starting with E and has less than 4 characters in total are not matched, and will not be modified.
If ignoring case: df$A <- sub("((?:^[Ee].|^[^Ee]).{2})(.+)", "\\1.\\2", df$A).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With