Reputation: 75
I have a column in a dataset where there are values referring to an economic amount. For example, 50.67 thousand euros, 78 thousand euros, 90 euros... All of them referring to thousand euros (the case where "euros" is written is obviously a mistake
I want to implement some code in R that allows me to remove the text that accompanies the numerical value and, instead, to have k€. For example 50.67 k€, 79 k€, 90 k€...
I have thought of doing it in the following way, however, in some records it says "thousand euros" or "euros" and therefore it cannot be done in a homogeneous way for all the values of a very long column.
Dataframe$column=gsub("\\thousand euros", "\\k€", as.character(Dataframe$column))
Any better idea?
Thank you
Upvotes: 0
Views: 39
Reputation: 700
A two-step solution with stringr
could be:
library(stringr)
library(dplyr)
Dataframe <- data.frame(column = c("50.67 thousand euros", "78 thousand euros", "90 euros"))
Dataframe %>% mutate(column = str_remove_all(column, "[^0-9.]"), column = paste(column, "k€"))
column
1 50.67 k€
2 78 k€
3 90 k€
Upvotes: 1