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