简体   繁体   English

从R中的数据框中删除0列

[英]Remove 0 columns from a data frame in R

I see this question has been asked before but the solutions provided by them gives some strange results in my case. 我看到这个问题以前曾被问过,但就我而言,他们提供的解决方案却给出了一些奇怪的结果。

my data frame (df) is this 我的数据框(df)是这个

         Department1  Department2  Department3  Cafeteria  Lobby(TT)  Lobby(Music Band)
  James  0            1            0            0          0          0
  Flynn  0            1            0            0          0          0
  Liam   0            1            0            0          0          0

My desired result is 我想要的结果是

       Department2  
James  1           
Flynn  1           
Liam   1           

My code used to remove zero columns is 我用来删除零列的代码是

df <- df[, colSums(df != 0) > 0]

Above code was taken by this 上面的代码是由这个

https://stackoverflow.com/a/21530306/7857035 https://stackoverflow.com/a/21530306/7857035

The result get is 结果得到的是

1  1
2  1
3  1

Above code works when there are more than one column which contains different values other than zeros. 当有多于一列的列包含除零以外的其他值时,以上代码有效。 How to get the desired result in this case? 在这种情况下如何获得期望的结果?

The immediate fix to your problem is to use drop=FALSE when subsetting the data frame: 解决问题的直接方法是在设置数据帧子集时使用drop=FALSE

df <- df[, colSums(df != 0) > 0, drop=FALSE]

This will tell R not to coerce the data frame down to the lowest dimension, which in this case is a numeric vector. 这将告诉R不要将数据帧强制降低到最低维度,在这种情况下,最低维度是一个数字矢量。 As you seem to have already noticed, coercion would not be a problem if you have more than one non zero sum column. 正如您似乎已经注意到的那样,如果您有多个非零和列,那么强制将不是问题。

Demo 演示

I'm getting accustomed to using purrr::keep for similar tasks to this. 我已经习惯于使用purrr::keep来完成类似的任务。

library(tibble)
library(dplyr)
library(purrr)

df <- read.table(text = "
name   Department1  Department2  Department3  Cafeteria  lobby_TT   lobby_music
James  0            1            0            0          0          0
Flynn  0            1            0            0          0          0
Liam   0            1            0            0          0          0",
                 header = TRUE, stringsAsFactor = FALSE)

df %>%
  column_to_rownames("name") %>%
  keep(~all(.x) != 0)

#       Department2
# James           1
# Flynn           1
# Liam            1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM