Search code examples
rtrim

Trim spaces in a column


I want to write a query to take count from a Datatable called "singapore" for a particular condition

NAME   COUNTRY     GENDER  OWNHOUSE  NO of houses
X1     SINGAPORE    M       Y        1
X2     SINGAPORE    F       N        0
X3     SINGAPORE    M       N        0

Now when I try to take counts from this Dataframe for Gender="F" and ownhouse="Y" it does not take correct count as at some places Gender is written as gender="M ". I want to trim only the column GENDER.

Currently I am using below command but it trims all the columns

Singapore<- data.frame(lapply(Singapore, trimws), stringsAsFactors = FALSE)

Solution

  • To trim only a single column just use the function trimws on that column only

    Singapore$GENDER <- trimws(Singapore$GENDER)