R: Remove multiple empty columns of character variables

后端 未结 9 1224
轮回少年
轮回少年 2020-12-08 11:06

I have a data frame where all the variables are of character type. Many of the columns are completely empty, i.e. only the variable headers are there, but no values. Is ther

9条回答
  •  北海茫月
    2020-12-08 11:35

    It depends what you mean by empty: Is it NA or "", or can it even be " "? Something like this might work:

    df[,!apply(df, 2, function(x) all(gsub(" ", "", x)=="", na.rm=TRUE))]
    

提交回复
热议问题