I have the following DF:
id<-c("id1","id2","id3","id4","id5","id6")
out<-c("50","60","60 4d", "60.4","5823",NA)
cov<-c("Male","male","mále","Fe male","female","fema")
dat<-data.frame(id,out,cov)
I create two functions to help me organize and clean my df:
conv_number<-function(data,variable){
data<- data |> dplyr::mutate(variable = gsub(pattern = ",", replacement = ".", variable))
x<- data |> dplyr::mutate(variable = as.numeric(gsub("[^0-9.-]", "", variable)))
return (x)
}
clean_string<-function(data,variable){
data |> dplyr::mutate(variable = tolower(variable))
x<- data |> dplyr::mutate(variable = gsub("[^a-z]", "", variable))
return (x)
}
My intention with those functions is that they take a column of a dataset and make some transformations in the same column. So I use them like this:
prueba_1<-conv_number(dat,out)
prueba_1<-clean_string(dat,cov)
However this is not what they do, they create a new column called "variable". And, of course, in the second example the variable does not transform the characters to tolower.
What I'm missing here? Perhaps there is something wrong with the dplyr::mutate()
function?