-3

I have a data frame which has multiple rows and columns

Data       C1         C2         C3 
R1     12         33         11
R2     11         2           0
R3      3         3           0
R4      2         3           1

and so on..

How can i drop all the rows from the data frame for which the C3 values are non zero?

The output data should look like

Data       C1         C2         C3
R2     11         2           0
R3      3         3           0
Joe
  • 183
  • 5
  • 16
  • 1
    Look at `dplyr`package (functions `filter` for rows and `select` for columns) – Edu Jun 22 '16 at 16:00
  • The answer is already posted on stackoverflow, give a look to this post, this may help: http://stackoverflow.com/questions/8005154/conditionally-remove-dataframe-rows-with-r – Fabio Marroni Jun 22 '16 at 16:01
  • This is a super basic R process. You do not need any packages. See the dupe links. – lmo Jun 22 '16 at 16:02

3 Answers3

1

I. Using R Basics

  df <- data.frame(Data=c("R1","R2","R3","R4"),C1=c(12,11,3,2),C2=c(33,2,3,3),C3=c(11,0,0,1))
  df
  #   Data C1 C2 C3
  # 1   R1 12 33 11
  # 2   R2 11  2  0
  # 3   R3  3  3  0
  # 4   R4  2  3  1

  df[df$C3 != 0,]
  #   Data C1 C2 C3
  # 1   R1 12 33 11
  # 4   R4  2  3  1

II. Using dplyr package's pipe operator %>% and filter() function

 library(dplyr)
 df %>% 
 filter(C3 != 0)
 #       Data C1 C2 C3
 #     1   R1 12 33 11
 #     2   R4  2  3  1
Sowmya S. Manian
  • 3,723
  • 3
  • 18
  • 30
0

x[x$C3==0,] should do the trick:

> x
   C1 C2 C3
R1 12 33 11
R2 11  2  0
R3  3  3  0
R4  2  3  1

> x[x$C3==0,]
   C1 C2 C3
R2 11  2  0
R3  3  3  0 
zohsvosv
  • 89
  • 1
  • 2
  • 12
0

Use filter from dplyr

df <- 
  df %>% 
  filter(C3 == 0)

Source: local data frame [2 x 4]

   Data    C1    C2    C3
  <chr> <int> <int> <int>
1    R2    11     2     0
2    R3     3     3     0

to reproduce your output data , however you asked

How can i drop all the rows from the data frame for which the C3 values are non zero?

in which case the code is

df <- 
  df %>% 
  filter(C3 != 0)

Source: local data frame [2 x 4]

       Data    C1    C2    C3
      <chr> <int> <int> <int>
    1    R1    12    33    11
    2    R4     2     3     1
sorearm
  • 409
  • 2
  • 10