Removing Whitespace From a Whole Data Frame in R

后端 未结 10 598
孤街浪徒
孤街浪徒 2020-12-05 03:01

I\'ve been trying to remove the white space that I have in a data frame (using R). The data frame is large (>1gb) and has multiple columns that contains whi

10条回答
  •  粉色の甜心
    2020-12-05 03:41

    I think that a simple approach with sapply, also works, given a df like:

    dat<-data.frame(S=LETTERS[1:10],
                M=LETTERS[11:20],
                X=c(rep("A:A",3),"?","A:A ",rep("G:G",5)),
                Y=c(rep("T:T",4),"T:T ",rep("C:C",5)),
                Z=c(rep("T:T",4),"T:T ",rep("C:C",5)),
                N=c(1:3,'4 ','5 ',6:10),
                stringsAsFactors = FALSE)
    

    You will notice that dat$N is going to become class character due to '4 ' & '5 ' (you can check with class(dat$N))

    To get rid of the spaces on the numeic column simply convert to numeric with as.numeric or as.integer.

    dat$N<-as.numeric(dat$N)

    If you want to remove all the spaces, do:

    dat.b<-as.data.frame(sapply(dat,trimws),stringsAsFactors = FALSE)
    

    And again use as.numeric on col N (ause sapply will convert it to character)

    dat.b$N<-as.numeric(dat.b$N)
    

提交回复
热议问题