enter image description hereI'm working on a dataset. The dataset has an attribute called ad_id that 0 means useless to the dataset, so I want to remove all zeros in ad_id, remove ad_id == 0 in the column, any suggestions?
Asked
Active
Viewed 423 times
-2
-
Do you want to remove the full rows where `ad_id==0` or do you want to put something (for example `NA`) instead of `0` in the cells with `ad_id==0`? – iago Aug 27 '20 at 08:59
-
image updated, could you take a look? – Frost Aug 27 '20 at 09:09
-
What's the difference between the two images? – Terru_theTerror Aug 27 '20 at 09:15
-
@Terru_theTerror It would be much much better If you try and find duplicate answers first rather than answering duplicated questions especially as basic as this one. It really help keeps this site 'clean' . – Sotos Aug 27 '20 at 09:19
2 Answers
1
Your question is not so clear, but:
If you want to select only rows with ad_id different from 0, this is the way:
df<-data.frame(ad_id=c(0,1,0,1),
a=c(1,2,3,4))
df[df$ad_id!=0,]
ad_id a
2 1 2
4 1 4
If you want to remove/replace 0 values, try this:
df[df$ad_id==0,"ad_id"]=""
df
ad_id a
1 1
2 1 2
3 3
4 1 4

Terru_theTerror
- 4,918
- 2
- 20
- 39
0
without seeing a test df/vector it is difficult to be sure what exactly you mean but mybe the "which" function can help you?
df$ad_id_no0<-which[df$ad_id>0] # this gives you a vector where all rows with a value above 0 are represented.
new_vector_no0<-df$ad_id[df$ad_id_no0] #this gives you the vector with all the above 0 values
EDIT
@Terru_theTerrors answer is better

D.J
- 1,180
- 1
- 8
- 17