danbi danbi -3 years ago 172
R Question

How to delete specific rows that only contains certain numbers?

As an image that I attached, I want to delete rows (ID) that only contains number 9, but not rows that contain other numbers.

Does anyone know how to write r codes to do this?

enter image description here

Answer Source

We can use rowSums on the logical matrix (df1[-1]==9 - subset of dataset as the first column in 'ID') and check whether it is 0, then keep that row

df1[rowSums(df1[-1]==9)==0,]

If the condition is to keep rows that have numbers other than 9, then as @Rui Barradas, mentioned

df1[rowSums(df1[-1] != 9) > 0,]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download