Reputation: 441
If i have a data frame like so
4 6
6 0
4 5
0 0
6 5
0 4
4 4
But a lot bigger, what command can I write that will delete any row that is completely 0's, such as row 4 in my example.
In the end it would look like this
4 6
6 0
4 5
6 5
0 4
4 4
Thanks!
Upvotes: 1
Views: 84
Reputation: 23788
Maybe this helps:
df <- df[-which(rowSums(abs(df)) == 0),]
#> df
# X1 X2
#1 4 0
#2 6 6
#3 6 5
#5 4 4
#6 5 4
#7 0 4
data
df <- data.frame(matrix(c(4, 6, 6, 0, 4, 5, 0, 0, 6, 5, 0, 4, 4, 4), ncol=2))
Upvotes: 1
Reputation: 330073
Something like this?
df[apply(df[, sapply(df, is.numeric)], 1, function(x) !all(x==0)), ]
or
df[rowSums(abs(df[, sapply(df, is.numeric)])) != 0, ]
Example:
> df <- data.frame(x=c(1, 2, 0), y=c(0, 1, 0), z=letters[1:3])
> df
x y z
1 1 0 a
2 2 1 b
3 0 0 c
> df[rowSums(abs(df[, sapply(df, is.numeric)])) != 0, ]
x y z
1 1 0 a
2 2 1 b
Upvotes: 4