I need to create a function that has an optional parameter, depending on if the frequencies are given or have to be calculated: Example tables:
If they are given:
tbl <- data.frame(
campoALimp = c('uno', "uno1", "Maria", "Mariana", "María", "Mara"),
freqAbs = c(2, 5, 2, 6, 7, 6))
If not:
tbl1 <- data.frame(campoALimp = tbl[rep(1:nrow(tbl), tbl[ , 2]), 1])
My function (part of it) is:
limpio <- function (tabla, campo, campo_conteo){
tabla <- tabla[nchar(as.character(tabla[, campo])) > 2, ]
if(missing(campo_conteo))
{ print("calcula freq")
#detach("package:plyr", unload=TRUE)
require(dplyr)
tabla1<-data.frame(tabla %>%
group_by_(campo) %>% summarise(frecuencia = n() ))
} else {tabla1 <- tabla
tabla1$frecuencia <- tabla1[, campo_conteo]}
return(tabla1)
}
First, I have problems with detach (in this case is commented but if I use it it shows error:
Error in detach("package:plyr", unload = TRUE) : invalid 'name' argument
If I run the code for the table with frequencies, I have no problem (it's only copying the original table).
limpio(tbl1, 'campoALimp')
But If I run it for the 2nd table: I got the following error:
limpio(tbl, 'campoALimp', 'freqAbs')
Error in UseMethod("group_by_") :
no applicable method for 'group_by_' applied to an object of class "factor"
I tried writing the detach plyr outside the function and the run the function, and I got the same error.
I tried doing the same outside the function:
tabla <- tbl1
campo <- 'campoALimp'
tabla1 <- NULL
tabla1 <- data.frame(tabla %>%
group_by_(campo) %>% summarise(frecuencia = n() ))
And I get the correct result
campoALimp frequency
Mara 6
Maria 2
María 7
Mariana 6
uno 2
uno1 5
Why this is not working inside the function? Thanks.