Skip to content
Prev 369539 / 398503 Next

Data import R: some explanatory variables not showing up correctly in summary

This function would strip any leading or trailing spaces from a column:

trim <-
   function (s) 
        {
    s <- as.character(s)
    s <- sub(pattern = "^[[:blank:]]+", replacement = "", x = s)
    s <- sub(pattern = "[[:blank:]]+$", replacement = "", x = s)
    s
         }

You could restrict it to non-mumeric columns with:

my_dfrm[ !sapply(my_dfrm, is.numeric) ] <- lapply( my_dfrm[ !sapply(my_dfrm, is.numeric) ], trim)

It would have the side-effect, (desirable in my opinion but opinions do vary on this matter), of converting any factor columns to character-class.
David Winsemius
Alameda, CA, USA