Hello i'm building an R function and would like to understand why one works and the other does not.
This one does not work
isola_por_fator_em_col <- function(data,col,fator)
{
y <- data[which(data$col==fator),]
x <- select_if(y,is.numeric)
summary(x)
}
isola_por_fator_em_col(data=desempenho_aluno_escola,col=priv,fator="privada")
Warning message:
Unknown or uninitialised column: `col`.
It also does not work when i type this
isola_por_fator_em_col(data=desempenho_aluno_escola,col="priv",fator="privada")
This one works
isola_por_fator_em_col <- function(data,col,fator)
{
y <- data[which(data[col]==fator),]
x <- select_if(y,is.numeric)
summary(x)
}
isola_por_fator_em_col(data=desempenho_aluno_escola,col="priv",fator="privada")
desempenho horas texp
Min. : 11.40 Min. : 4.00 Min. : 9.0
1st Qu.: 51.42 1st Qu.:16.00 1st Qu.: 9.0
Median : 67.45 Median :21.00 Median :10.0
Mean : 66.55 Mean :20.06 Mean :13.3
3rd Qu.: 82.47 3rd Qu.:25.00 3rd Qu.:19.0
Max. :108.00 Max. :31.00 Max. :20.0
Basically what is the difference between $ and [] in R. When i call the $ data$priv OUTSIDE the function it returns the column with no problem.
I think the [] returns the COLUMN while the $ returns the values, but i dont understand why comparing the values in the function would not work.
if i call
desempenho_aluno_escola[which(desempenho_aluno_escola$priv=="privada"),]
Outside the function it works normally