1

I'm trying to recreate a data frame (DC5_prod) that has hundreds of columns, but many without any values other than zero.

The first column in the data frame is text and the rest are numeric. Is there a way to ignore the first column while simultaneously eliminating the remainder of columns that are composed entirely of zeros?

DC5_Prod
 a   b c d e f   
1 AK 0 0 0 0 1 
2 JI 0 0 0 0 0 

The above is a snippet of how it currently stands and would want an output of:

DC5_Prod
 a    f   
1 AK  1 
2 JI  0 

When I attempt to utilize the solution issued on a similar question on the site:

DC5_prod[, colSums(DC5_prod != 0) > 0]

just essentially returns the first column without removing any.

Franchise
  • 1,081
  • 3
  • 15
  • 30
  • Using your original line of thought `DC5_Prod[,-(which(colSums(DC5_Prod[2:ncol(DC5_Prod)]) == 0)+1)]` not very elegant if more of your columns have characters. – Badger Mar 08 '18 at 20:46

1 Answers1

2

Try this R base approach

> ind <- sapply(DC5_Prod, function(x) sum(x==0)) != nrow(DC5_Prod)
> DC5_Prod[,ind]
   a f
1 AK 1
2 JI 0
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138