[R] Assigning a variable value based on multiple columns
Jason Stout, M.D.
jason.stout at duke.edu
Thu Apr 25 12:35:27 CEST 2013
Thanks Patrick--I think this solution will work perfectly.
Jason Stout, MD, MHS
Durham, NC 27710
From: Patrick Coulombe [patrick.coulombe at gmail.com]
Sent: Thursday, April 25, 2013 1:53 AM
To: Jason Stout, M.D.
Cc: r-help at r-project.org
Subject: Re: [R] Assigning a variable value based on multiple columns
I think that the easiest for you would be to keep your current elseif
statements as is, but change your NA into something else (e.g., -999,
or anything else). To do this in one line, you can use the package
In this code, I assume that your data are stored in the variable "dataset":
#install package gdata if not yet installed
#load package gdata
#change NA into -999
dataset <- NAToUnknown(dataset, -999)
#do your ifs/ifelses here...
#change -999 back into NA
dataset <- unknownToNA(dataset, -999)
And that should do it.
Hope this helps,
2013/4/24 Jason Stout, M.D. <jason.stout at duke.edu>
> Hi All,
> I'm hoping someone can help me with a relatively simple problem. Take the following dataset:
> ID Diabetes ESRD HIV Contact
> 1 0 0 NA 0
> 2 1 0 NA 0
> 3 NA 1 0 0
> 4 0 NA 0 1
> 5 1 1 1 0
> I want to generate a column called TSTcutoff based on the values in the row. TSTcutoff would be the lower of 15 (if Diabetes=ESRD=HIV=Contact=0), 10 (if Diabetes or ESRD=1 AND HIV=Contact=0), or 5 (if HIV OR Contact=1). I was thinking this could be done with a series of IFELSE statements, but the NA values make this more challenging. I want to ignore NA values when calculating TSTcutoff. So the final dataset should look like this:
> ID Diabetes ESRD HIV Contact TSTcutoff
> 1 0 0 NA 0 15
> 2 1 0 NA 0 10
> 3 NA 1 0 0 10
> 4 0 NA 0 1 5
> 5 1 1 1 0 5
> Thanks for any suggestions.
> Jason Stout, MD, MHS
> Box 102359-DUMC
> Durham, NC 27710
> FAX 919-681-7494
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help