I have multiple columns in a table called "Gr1","Gr2",...,"Gr10".
I want to convert the class from character to integer. I want to do it in a dynamic way, I'm trying this, but it doesn't work:
for (i in 1:10) {
Col <- paste0('Students1$Gr',i)
Col <- as.integer(Col)
}
My objective here is to know how to add dynamically the for variable to the name of a column. Something like:
for (i in 1:10) {
Students1$Gr(i) <- as.integer(Students1$Gr(i))
}
Any idea is welcome. Thank you very much, Matias
charactertointeger? is it this what you want? your question is not so clear to me.*apply. Something like:cols <- names(Students)[grepl("Gr", names(Students))]; Students[, cols] <- lapply(Students[,cols], as.integer)sapplymight be better thanlapplysince we are not working with lists here.data.frames are lists with a few additional properties (seeis.list(data.frame(a=1))). We are trying to replace list elements (the numeric vectors) with other elements (character vectors).sapplywould work because it'll return a matrix, but then that has to be put back into list elements when you assign it to thedata.frame. Also I dislikesapplybecause it simplifies the result for you.