I want to convert my NA into 0 and other values into 1 without affecting my ID variable
Asked
Active
Viewed 286 times
-1
-
4Could you make some effort and provide a reproducible example? – May 26 '15 at 05:58
-
1In addition to the comment of @Pascal: some info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to produce a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). This will make it much easier for others to help you. – Jaap May 26 '15 at 06:17
-
@ arkun i did not know about the image , please accept my apology for downvote – Saugandh Datta May 26 '15 at 10:21
-
1It's okay. I knew that being new to the site, you were not aware of it. Not a big deal. Otherwise, your question and expected answer is clear. – akrun May 26 '15 at 10:30
1 Answers
8
You can try
df1[-1] <- (!is.na(df1[-1]))+0L
df1
# ID condimenti occhialli pane prontoTaway surgelati utensili
#1 1 0 0 1 0 0 0
#2 2 0 1 0 0 0 0
#3 3 0 0 0 0 1 0
#4 4 0 0 0 0 1 0
Or using data.table
library(data.table)
setDT(df1)
for(j in 2:ncol(df1)){
set(df1, i= NULL, j=j, value= (is.na(df1[[j]]))+0L)
}
Or using dplyr
library(dplyr)
df1 %>%
mutate_each(funs((!is.na(.))+0L), -ID)
data
df1 <- structure(list(ID = 1:4, condimenti = c(NA, NA, NA, NA),
occhialli = c(NA,
"CONDIMENTI PRIMI (PELATI & SUGHI)", NA, NA), pane =
c("OCCHIALI E OROLOGI",
NA, NA, NA), prontoTaway = c(NA, NA, NA, NA), surgelati = c(NA,
NA, "PRONTO CALDO TAKE AWAY", "PRONTO CALDO TAKe AWAY"), utensili = c(NA,
NA, NA, NA)), .Names = c("ID", "condimenti", "occhialli", "pane",
"prontoTaway", "surgelati", "utensili"), row.names = c(NA, -4L
), class = "data.frame")

akrun
- 874,273
- 37
- 540
- 662
-
1From my point of you, your answer exempts the OP to make some effort to ask reasonable question. You did all the work for him. – May 26 '15 at 06:12
-
4@Pascal I did look at his other posts where he provided an example data, but I think being a newbie, he may not know whether an image is suitable or not. But, downvoting for a correct answer is not good. – akrun May 26 '15 at 06:14
-
-
-
@Pascal if I look at a recent answer by you, http://stackoverflow.com/questions/30448709/subtract-from-the-previous-row-r/30448772#30448772 it may also have duplicates and if the OP made some effort, he could have easily find it by himself – akrun May 26 '15 at 06:17
-
So you waste your time searching recent answers I gave. Up to you. Feel free to downvote all my answers. – May 26 '15 at 06:19
-
@Pascal No, I saw that some time ago, just remembered because you made a point. Anyway, it is your choice and I respect it. – akrun May 26 '15 at 06:19
-
@Pascal Also, from the OP's post, the question is reasonable and he did stated it clearly and that is position. – akrun May 26 '15 at 06:25
-
We have a different viewpoint on what is a reasonable question. Anyway, your answer provides what the OP is looking for. For the duplicate, I usually do check before answering. It seems I failed for this one. I "repaired" the fault. – May 26 '15 at 06:29
-
2@Pascal I wouldn't delete an answer just because somebody found it a duplicate. But, it is up to you. Also, whether I waste time or not, it is completely up to me. – akrun May 26 '15 at 07:11
-
-
1my 2c: [Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect.](http://stackoverflow.com/help/privileges/vote-down) – user20650 May 29 '15 at 01:14