In R, create a function to change some levels of a variable

Asked

Viewed 995 times

5

I want to create a function that facilitates my life when treating some variables

I want a function that receives as input a database, a column or variable u of that database, a vector c specifying the levels to be exchanged and the new name that will replace such levels.

Actually, I tried to do this directly with relevel function, but I didn’t find it very easy to use.

I created a function that returns me a vector type factor... but actually, I want the function to transform the data Matrix so that the variable u is already modified... because after using my function and I give a levels(data$u) appear the old levels

  juntar<- function(data, u, c , novolevel)
 {

 ### Trasformamos nossa variável em tipo character
 data[,which(colnames(data)== u )]<- as.character(data[,which(colnames(data)== u )])

 levels<- c

 ### determinamos as coordenadas levels
 coordenadas_levels<- data[,which(colnames(data)== u )] %in% levels
 coordenadas_levels<- which(coordenadas_levels == TRUE)

 ### Fazemos a mudança
 data[,which(colnames(data)== u )][coordenadas_levels]<- novolevel

 ### Convertemos em factor
 data[,which(colnames(data)== u )]<- as.factor(data[,which(colnames(data)== u )])

 }

Thank you

1 answer

4


I did the following function:

juntar <- function(dat, variavel, levls, novosLevls){
  # pega o nome da variável
  variavel <- deparse(substitute(variavel))
  # cria data frame auxiliar para dar merge
  aux <- data.frame(x = levls, .Novo = novosLevls)
  names(aux)[1] <- variavel

  # merge desse novo data frame
  dat <- merge(x = dat, y = aux, by = variavel, all.x = T)
  # substitui a variavel species pela nova
  dat[[variavel]] <- ifelse(is.na(dat$.Novo), as.character(dat[[variavel]]), as.character(dat$.Novo))
  # exclui a variavel nova
  exc <- names(dat) == ".Novo"
  dat <- dat[!exc]
  # retorna o data frame
  return(dat)
}

I think it’s working. I tested it with the date. iris thus:

juntar(iris, Species, c("setosa", "virginica"), "novo") %>% head

  Species Sepal.Length Sepal.Width Petal.Length Petal.Width
1    novo          4.9         3.1          1.5         0.1
2    novo          5.0         3.4          1.5         0.2
3    novo          4.4         2.9          1.4         0.2
4    novo          5.1         3.5          1.4         0.2
5    novo          4.9         3.0          1.4         0.2
6    novo          4.7         3.2          1.3         0.2

The last argument novosLvls can be either a vector of the same size as the levls as a single value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.