Removing only blank cells in R (not the entire row or column)

后端 未结 1 896
小蘑菇
小蘑菇 2021-01-27 05:58

So I want to remove all cells that are blank within my dataset. So for example, if I had something like this, where the (..) represents blank numbers:-

1 10 .. 4         


        
相关标签:
1条回答
  • 2021-01-27 06:49

    If your data.frame is named df then:

    df[df == ".."] <- NA
    

    In order to get NAs, then:

    func<-function(i){
      x<-as.numeric(as.character(df[,i][!is.na(df[,i])]))
      xna<-as.numeric(as.character(df[,i][is.na(df[,i])]))
      newx<-c(x,xna)
    }
    
    do.call(cbind,lapply(1:length(df[1,]),func))
    
    > do.call(cbind,lapply(1:length(df[1,]),func))
         [,1] [,2] [,3] [,4]
    [1,]    1   10   10    4
    [2,]   14    9   12    8
    [3,]   NA    8   NA   16
    
    0 讨论(0)
提交回复
热议问题