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

5

I want to create a function that makes my life easier when handling some variables

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

Actually, I tried to do this directly with the relevel function, but I did not find it very easy to use.

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

  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

    
asked by anonymous 30.01.2015 / 05:01

1 answer

4

I've done 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 data.set iris like this:

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 levls as a single value.

    
30.01.2015 / 14:53