IF Statement with multiple conditions always saying true (nested in while loop)data.table vs dplyr: can one do something well the other can't or does poorly?Issues with nested while loop in for loop for RIfelse statment across multiple rowsIf value is in another dataframe, replace multiple columns with NAElegant way to do nested if else statements for multiple groupsifelse inside for loop w/ 2 conditionsIncrease efficiency of for loop with conditional logicMy if..else code is failingUpdating a vector outside a For Loop based on dataframe column conditionDataframe change names of factors if frequency of the factor is lower than some number
Square spiral in Mathematica
Why is vowel phonology represented in a trapezoid instead of a square?
refer string as a field API name
Why is Drogon so much better in battle than Rhaegal and Viserion?
Is it standard to have the first week's pay indefinitely withheld?
How can we delete item permanently without storing in Recycle Bin?
Physically unpleasant work environment
Why use a retrograde orbit?
A latin word for "area of interest"
How could it be that 80% of townspeople were farmers during the Edo period in Japan?
Cycling to work - 30mile return
How was the blinking terminal cursor invented?
Holding rent money for my friend which amounts to over $10k?
Does a non-singular matrix have a large minor with disjoint rows and columns and full rank?
Polynomial division: Is this trick obvious?
What dog breeds survive the apocalypse for generations?
Why do academics prefer Mac/Linux?
Why can't I share a one use code with anyone else?
Failing students when it might cause them economic ruin
Usage of the relative pronoun "dont"
How does the Heat Metal spell interact with a follow-up Frostbite spell?
What do astronauts do with their trash on the ISS?
How can I make dummy text (like lipsum) grey?
How does this piece of code determine array size without using sizeof( )?
IF Statement with multiple conditions always saying true (nested in while loop)
data.table vs dplyr: can one do something well the other can't or does poorly?Issues with nested while loop in for loop for RIfelse statment across multiple rowsIf value is in another dataframe, replace multiple columns with NAElegant way to do nested if else statements for multiple groupsifelse inside for loop w/ 2 conditionsIncrease efficiency of for loop with conditional logicMy if..else code is failingUpdating a vector outside a For Loop based on dataframe column conditionDataframe change names of factors if frequency of the factor is lower than some number
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Please see my code below.
I have a data frame where each race a person identifies with is in a column (ex. AWHITE, ABLACK, etc.) and if they identify with this race then the entry is 1 (if not the entry is 2). Respondents can identify with more than one race.
I'm trying to identify when a respondent has stated they belong to more than one race. If they do, I want one column (ARACE) to update to 91 and another column (AOTHRACE) to become 2.
The if statement (in the code below) is always evaluating to TRUE. Even though this is not correct. There are respondents who identify as only one race (i.e. white). I've looked this over multiple times, but I can't find where I've messed up.
I plan to use more if statements (if, else if) for the other races/columns as well (i.e. a respondent identifies as black AND at least one other race), but I can't even get the first one to work, so I haven't implemented that.
(I don't actually want the else to be 0, I just used that to confirm the code wasn't working as expected. When I ran the summary function on ARACE the minimum was 91, so I know this statement was never evaluated.)
i <- 0
while (i <= nrow(nhes05v2))
if ((nhes05v2$AWHITE == 1) && (any(nhes05v2$ABLACK==1, nhes05v2$AAMIND==1, nhes05v2$AASIAN==1, nhes05v2$APACI==1)))
nhes05v2$ARACE = 91
nhes05v2$AOTHRACE = 2
else nhes05v2$ARACE = 0
nhes05v2$AOTHRACE = 0
i <- i+1
Here's an example of the values:
> nhes05v2$AWHITE[1:20]
[1] 1 1 1 1 1 1 2 2 2 1 1 2 1 1 1 1 1 1 1 1
> nhes05v2$ABLACK[1:20]
[1] 2 1 2 2 2 2 1 1 1 2 2 1 2 2 2 2 2 2 2 2
> nhes05v2$AASIAN[1:20]
[1] 2 2 2 1 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2
> nhes05v2$AAMIND[1:20]
[1] 2 2 2 2 2 1 2 2 2 2 2 2 2 1 2 2 2 2 2 2
> nhes05v2$APACI[1:20]
[1] 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2
I would like the output to be something like (this would be with more than just my one if statement above, there'd be more if, else if
, but since I'm stuck on the first I haven't gone past that)
> nhes05v2$ARACE[1:20]
[1] 0 91 0 91 0 91 91 0 0 91 0 0 0 0 0 0 0 0 0 0
> nhes05v2$AOTHRACE[1:20]
[1] 0 2 0 2 0 2 2 0 0 2 0 0 0 0 0 0 0 0 0 0
Currently the output is
> nhes05v2$ARACE[1:20]
[1] 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91
> nhes05v2$AOTHRACE[1:20]
[1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
r
|
show 2 more comments
Please see my code below.
I have a data frame where each race a person identifies with is in a column (ex. AWHITE, ABLACK, etc.) and if they identify with this race then the entry is 1 (if not the entry is 2). Respondents can identify with more than one race.
I'm trying to identify when a respondent has stated they belong to more than one race. If they do, I want one column (ARACE) to update to 91 and another column (AOTHRACE) to become 2.
The if statement (in the code below) is always evaluating to TRUE. Even though this is not correct. There are respondents who identify as only one race (i.e. white). I've looked this over multiple times, but I can't find where I've messed up.
I plan to use more if statements (if, else if) for the other races/columns as well (i.e. a respondent identifies as black AND at least one other race), but I can't even get the first one to work, so I haven't implemented that.
(I don't actually want the else to be 0, I just used that to confirm the code wasn't working as expected. When I ran the summary function on ARACE the minimum was 91, so I know this statement was never evaluated.)
i <- 0
while (i <= nrow(nhes05v2))
if ((nhes05v2$AWHITE == 1) && (any(nhes05v2$ABLACK==1, nhes05v2$AAMIND==1, nhes05v2$AASIAN==1, nhes05v2$APACI==1)))
nhes05v2$ARACE = 91
nhes05v2$AOTHRACE = 2
else nhes05v2$ARACE = 0
nhes05v2$AOTHRACE = 0
i <- i+1
Here's an example of the values:
> nhes05v2$AWHITE[1:20]
[1] 1 1 1 1 1 1 2 2 2 1 1 2 1 1 1 1 1 1 1 1
> nhes05v2$ABLACK[1:20]
[1] 2 1 2 2 2 2 1 1 1 2 2 1 2 2 2 2 2 2 2 2
> nhes05v2$AASIAN[1:20]
[1] 2 2 2 1 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2
> nhes05v2$AAMIND[1:20]
[1] 2 2 2 2 2 1 2 2 2 2 2 2 2 1 2 2 2 2 2 2
> nhes05v2$APACI[1:20]
[1] 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2
I would like the output to be something like (this would be with more than just my one if statement above, there'd be more if, else if
, but since I'm stuck on the first I haven't gone past that)
> nhes05v2$ARACE[1:20]
[1] 0 91 0 91 0 91 91 0 0 91 0 0 0 0 0 0 0 0 0 0
> nhes05v2$AOTHRACE[1:20]
[1] 0 2 0 2 0 2 2 0 0 2 0 0 0 0 0 0 0 0 0 0
Currently the output is
> nhes05v2$ARACE[1:20]
[1] 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91
> nhes05v2$AOTHRACE[1:20]
[1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
r
Are your race variables (AWHITE
,ABLACK
,AAMIND
, etc.) coded as numeric or factor levels?
– NM_
Mar 23 at 16:18
Your code is not reproducible and testable. Please share a reproducible example of your data frame and the expected output. It is likely that we don't need any for-loop or while-loop to achieve your task.
– www
Mar 23 at 16:18
@NM_ they are currently numeric
– jazlaw
Mar 23 at 16:32
@www I've edited to add some context. Thank you both for your feedback!
– jazlaw
Mar 23 at 16:33
In order to belong to more than one race, is it possible for some one to be of two non-white races (for example, Asian and Black)? This is because your current if statement considers that a person is more than one race if they are white + at least one more race (i.e. in order to be mixed, that have to be white + some other race).
– NM_
Mar 23 at 16:48
|
show 2 more comments
Please see my code below.
I have a data frame where each race a person identifies with is in a column (ex. AWHITE, ABLACK, etc.) and if they identify with this race then the entry is 1 (if not the entry is 2). Respondents can identify with more than one race.
I'm trying to identify when a respondent has stated they belong to more than one race. If they do, I want one column (ARACE) to update to 91 and another column (AOTHRACE) to become 2.
The if statement (in the code below) is always evaluating to TRUE. Even though this is not correct. There are respondents who identify as only one race (i.e. white). I've looked this over multiple times, but I can't find where I've messed up.
I plan to use more if statements (if, else if) for the other races/columns as well (i.e. a respondent identifies as black AND at least one other race), but I can't even get the first one to work, so I haven't implemented that.
(I don't actually want the else to be 0, I just used that to confirm the code wasn't working as expected. When I ran the summary function on ARACE the minimum was 91, so I know this statement was never evaluated.)
i <- 0
while (i <= nrow(nhes05v2))
if ((nhes05v2$AWHITE == 1) && (any(nhes05v2$ABLACK==1, nhes05v2$AAMIND==1, nhes05v2$AASIAN==1, nhes05v2$APACI==1)))
nhes05v2$ARACE = 91
nhes05v2$AOTHRACE = 2
else nhes05v2$ARACE = 0
nhes05v2$AOTHRACE = 0
i <- i+1
Here's an example of the values:
> nhes05v2$AWHITE[1:20]
[1] 1 1 1 1 1 1 2 2 2 1 1 2 1 1 1 1 1 1 1 1
> nhes05v2$ABLACK[1:20]
[1] 2 1 2 2 2 2 1 1 1 2 2 1 2 2 2 2 2 2 2 2
> nhes05v2$AASIAN[1:20]
[1] 2 2 2 1 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2
> nhes05v2$AAMIND[1:20]
[1] 2 2 2 2 2 1 2 2 2 2 2 2 2 1 2 2 2 2 2 2
> nhes05v2$APACI[1:20]
[1] 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2
I would like the output to be something like (this would be with more than just my one if statement above, there'd be more if, else if
, but since I'm stuck on the first I haven't gone past that)
> nhes05v2$ARACE[1:20]
[1] 0 91 0 91 0 91 91 0 0 91 0 0 0 0 0 0 0 0 0 0
> nhes05v2$AOTHRACE[1:20]
[1] 0 2 0 2 0 2 2 0 0 2 0 0 0 0 0 0 0 0 0 0
Currently the output is
> nhes05v2$ARACE[1:20]
[1] 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91
> nhes05v2$AOTHRACE[1:20]
[1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
r
Please see my code below.
I have a data frame where each race a person identifies with is in a column (ex. AWHITE, ABLACK, etc.) and if they identify with this race then the entry is 1 (if not the entry is 2). Respondents can identify with more than one race.
I'm trying to identify when a respondent has stated they belong to more than one race. If they do, I want one column (ARACE) to update to 91 and another column (AOTHRACE) to become 2.
The if statement (in the code below) is always evaluating to TRUE. Even though this is not correct. There are respondents who identify as only one race (i.e. white). I've looked this over multiple times, but I can't find where I've messed up.
I plan to use more if statements (if, else if) for the other races/columns as well (i.e. a respondent identifies as black AND at least one other race), but I can't even get the first one to work, so I haven't implemented that.
(I don't actually want the else to be 0, I just used that to confirm the code wasn't working as expected. When I ran the summary function on ARACE the minimum was 91, so I know this statement was never evaluated.)
i <- 0
while (i <= nrow(nhes05v2))
if ((nhes05v2$AWHITE == 1) && (any(nhes05v2$ABLACK==1, nhes05v2$AAMIND==1, nhes05v2$AASIAN==1, nhes05v2$APACI==1)))
nhes05v2$ARACE = 91
nhes05v2$AOTHRACE = 2
else nhes05v2$ARACE = 0
nhes05v2$AOTHRACE = 0
i <- i+1
Here's an example of the values:
> nhes05v2$AWHITE[1:20]
[1] 1 1 1 1 1 1 2 2 2 1 1 2 1 1 1 1 1 1 1 1
> nhes05v2$ABLACK[1:20]
[1] 2 1 2 2 2 2 1 1 1 2 2 1 2 2 2 2 2 2 2 2
> nhes05v2$AASIAN[1:20]
[1] 2 2 2 1 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2
> nhes05v2$AAMIND[1:20]
[1] 2 2 2 2 2 1 2 2 2 2 2 2 2 1 2 2 2 2 2 2
> nhes05v2$APACI[1:20]
[1] 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2
I would like the output to be something like (this would be with more than just my one if statement above, there'd be more if, else if
, but since I'm stuck on the first I haven't gone past that)
> nhes05v2$ARACE[1:20]
[1] 0 91 0 91 0 91 91 0 0 91 0 0 0 0 0 0 0 0 0 0
> nhes05v2$AOTHRACE[1:20]
[1] 0 2 0 2 0 2 2 0 0 2 0 0 0 0 0 0 0 0 0 0
Currently the output is
> nhes05v2$ARACE[1:20]
[1] 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91
> nhes05v2$AOTHRACE[1:20]
[1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
r
r
edited Mar 23 at 16:57
jazlaw
asked Mar 23 at 16:13
jazlawjazlaw
84
84
Are your race variables (AWHITE
,ABLACK
,AAMIND
, etc.) coded as numeric or factor levels?
– NM_
Mar 23 at 16:18
Your code is not reproducible and testable. Please share a reproducible example of your data frame and the expected output. It is likely that we don't need any for-loop or while-loop to achieve your task.
– www
Mar 23 at 16:18
@NM_ they are currently numeric
– jazlaw
Mar 23 at 16:32
@www I've edited to add some context. Thank you both for your feedback!
– jazlaw
Mar 23 at 16:33
In order to belong to more than one race, is it possible for some one to be of two non-white races (for example, Asian and Black)? This is because your current if statement considers that a person is more than one race if they are white + at least one more race (i.e. in order to be mixed, that have to be white + some other race).
– NM_
Mar 23 at 16:48
|
show 2 more comments
Are your race variables (AWHITE
,ABLACK
,AAMIND
, etc.) coded as numeric or factor levels?
– NM_
Mar 23 at 16:18
Your code is not reproducible and testable. Please share a reproducible example of your data frame and the expected output. It is likely that we don't need any for-loop or while-loop to achieve your task.
– www
Mar 23 at 16:18
@NM_ they are currently numeric
– jazlaw
Mar 23 at 16:32
@www I've edited to add some context. Thank you both for your feedback!
– jazlaw
Mar 23 at 16:33
In order to belong to more than one race, is it possible for some one to be of two non-white races (for example, Asian and Black)? This is because your current if statement considers that a person is more than one race if they are white + at least one more race (i.e. in order to be mixed, that have to be white + some other race).
– NM_
Mar 23 at 16:48
Are your race variables (
AWHITE
, ABLACK
, AAMIND
, etc.) coded as numeric or factor levels?– NM_
Mar 23 at 16:18
Are your race variables (
AWHITE
, ABLACK
, AAMIND
, etc.) coded as numeric or factor levels?– NM_
Mar 23 at 16:18
Your code is not reproducible and testable. Please share a reproducible example of your data frame and the expected output. It is likely that we don't need any for-loop or while-loop to achieve your task.
– www
Mar 23 at 16:18
Your code is not reproducible and testable. Please share a reproducible example of your data frame and the expected output. It is likely that we don't need any for-loop or while-loop to achieve your task.
– www
Mar 23 at 16:18
@NM_ they are currently numeric
– jazlaw
Mar 23 at 16:32
@NM_ they are currently numeric
– jazlaw
Mar 23 at 16:32
@www I've edited to add some context. Thank you both for your feedback!
– jazlaw
Mar 23 at 16:33
@www I've edited to add some context. Thank you both for your feedback!
– jazlaw
Mar 23 at 16:33
In order to belong to more than one race, is it possible for some one to be of two non-white races (for example, Asian and Black)? This is because your current if statement considers that a person is more than one race if they are white + at least one more race (i.e. in order to be mixed, that have to be white + some other race).
– NM_
Mar 23 at 16:48
In order to belong to more than one race, is it possible for some one to be of two non-white races (for example, Asian and Black)? This is because your current if statement considers that a person is more than one race if they are white + at least one more race (i.e. in order to be mixed, that have to be white + some other race).
– NM_
Mar 23 at 16:48
|
show 2 more comments
2 Answers
2
active
oldest
votes
We can achieve this by recoding the 2 to 0 (i.e, 0 = "No") and using the following code with 2 functions used to determine whether a record satisfies criteria.
Please note that the code assumes that the race variables are numeric.
# Replicate your example
AWHITE = as.numeric(unlist(strsplit("1 1 1 1 1 1 2 2 2 1 1 2 1 1 1 1 1 1 1 1", " ")))
ABLACK = as.numeric(unlist(strsplit("2 1 2 2 2 2 1 1 1 2 2 1 2 2 2 2 2 2 2 2", " ")))
AASIAN = as.numeric(unlist(strsplit("2 2 2 1 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2", " ")))
AAMIND = as.numeric(unlist(strsplit("2 2 2 2 2 1 2 2 2 2 2 2 2 1 2 2 2 2 2 2", " ")))
APACI = as.numeric(unlist(strsplit("2 2 2 2 2 1 2 2 2 2 2 2 2 1 2 2 2 2 2 2", " ")))
nhes05v2 = data.frame(AWHITE, ABLACK, AASIAN, AAMIND, APACI)
> nhes05v2 # Partial output given
AWHITE ABLACK AASIAN AAMIND APACI
1 1 2 2 2 2
2 1 1 2 2 2
3 1 2 2 2 2
...
18 1 2 2 2 2
19 1 2 2 2 2
20 1 2 2 2 2
Recode the variables
# Recode variables. Change all 2's to 0's (New coding is 1 = Yes, 0 = No).
nhes05v2[nhes05v2 == 2] = 0
Create 2 functions to satisfy criteria for ARACE
and AORACE
# A person is mixed race it they answer 1 to more than one race
# Therefore a person whose row sum is greater than 1 is mixed race
determine.arace = function(AWHITE, ABLACK, AAMIND, AASIAN, APACI)
ifelse( sum(AWHITE, ABLACK, AAMIND, AASIAN, APACI ) > 1 , 91, 0)
determine.aothrace = function(AWHITE, ABLACK, AAMIND, AASIAN, APACI)
ifelse( sum(AWHITE, ABLACK, AAMIND, AASIAN, APACI ) > 1 , 2, 0)
Apply these functions to your data
ARACE = mapply(determine.arace, nhes05v2$AWHITE, nhes05v2$ABLACK, nhes05v2$AAMIND, nhes05v2$AASIAN, nhes05v2$APACI)
> ARACE
[1] 0 91 0 91 0 91 91 0 0 0 0 0 0 91 0 0 0 0 0 0
AOTHRACE = mapply(determine.aothrace, nhes05v2$AWHITE, nhes05v2$ABLACK, nhes05v2$AAMIND, nhes05v2$AASIAN, nhes05v2$APACI)
> AOTHRACE
[1] 0 2 0 2 0 2 2 0 0 0 0 0 0 2 0 0 0 0 0 0
To add them to your data frame
nhes05v2$ARACE = ARACE
nhes05v2$AOTHRACE = AOTHRACE
Thank you so much! This easily gets me to what I need for all that extra code! And thank you for your comments to help clarify my question.
– jazlaw
Mar 24 at 9:23
@jazlaw, glad to hear this! Good luck with your research :)
– NM_
Mar 24 at 19:28
add a comment |
Using the dplyr
and magrittr
packages, my best version of this looks like:
nhes05v2 %>%
mutate(ARACE = ifelse(AWHITE == 1 & (ABLACK == 1 | AAMIND ==1 | AASIAN == 1 | APACI == 1), 91, 0),
AOTHRACE = ifelse(AWHITE == 1 & (ABLACK == 1 | AAMIND ==1 | AASIAN == 1 | APACI == 1), 2, 0))
The && conditional in R only checks against the first row, which is why you got the results you did here - here's a post from other people who have been flummoxed by that behavior.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55315756%2fif-statement-with-multiple-conditions-always-saying-true-nested-in-while-loop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
We can achieve this by recoding the 2 to 0 (i.e, 0 = "No") and using the following code with 2 functions used to determine whether a record satisfies criteria.
Please note that the code assumes that the race variables are numeric.
# Replicate your example
AWHITE = as.numeric(unlist(strsplit("1 1 1 1 1 1 2 2 2 1 1 2 1 1 1 1 1 1 1 1", " ")))
ABLACK = as.numeric(unlist(strsplit("2 1 2 2 2 2 1 1 1 2 2 1 2 2 2 2 2 2 2 2", " ")))
AASIAN = as.numeric(unlist(strsplit("2 2 2 1 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2", " ")))
AAMIND = as.numeric(unlist(strsplit("2 2 2 2 2 1 2 2 2 2 2 2 2 1 2 2 2 2 2 2", " ")))
APACI = as.numeric(unlist(strsplit("2 2 2 2 2 1 2 2 2 2 2 2 2 1 2 2 2 2 2 2", " ")))
nhes05v2 = data.frame(AWHITE, ABLACK, AASIAN, AAMIND, APACI)
> nhes05v2 # Partial output given
AWHITE ABLACK AASIAN AAMIND APACI
1 1 2 2 2 2
2 1 1 2 2 2
3 1 2 2 2 2
...
18 1 2 2 2 2
19 1 2 2 2 2
20 1 2 2 2 2
Recode the variables
# Recode variables. Change all 2's to 0's (New coding is 1 = Yes, 0 = No).
nhes05v2[nhes05v2 == 2] = 0
Create 2 functions to satisfy criteria for ARACE
and AORACE
# A person is mixed race it they answer 1 to more than one race
# Therefore a person whose row sum is greater than 1 is mixed race
determine.arace = function(AWHITE, ABLACK, AAMIND, AASIAN, APACI)
ifelse( sum(AWHITE, ABLACK, AAMIND, AASIAN, APACI ) > 1 , 91, 0)
determine.aothrace = function(AWHITE, ABLACK, AAMIND, AASIAN, APACI)
ifelse( sum(AWHITE, ABLACK, AAMIND, AASIAN, APACI ) > 1 , 2, 0)
Apply these functions to your data
ARACE = mapply(determine.arace, nhes05v2$AWHITE, nhes05v2$ABLACK, nhes05v2$AAMIND, nhes05v2$AASIAN, nhes05v2$APACI)
> ARACE
[1] 0 91 0 91 0 91 91 0 0 0 0 0 0 91 0 0 0 0 0 0
AOTHRACE = mapply(determine.aothrace, nhes05v2$AWHITE, nhes05v2$ABLACK, nhes05v2$AAMIND, nhes05v2$AASIAN, nhes05v2$APACI)
> AOTHRACE
[1] 0 2 0 2 0 2 2 0 0 0 0 0 0 2 0 0 0 0 0 0
To add them to your data frame
nhes05v2$ARACE = ARACE
nhes05v2$AOTHRACE = AOTHRACE
Thank you so much! This easily gets me to what I need for all that extra code! And thank you for your comments to help clarify my question.
– jazlaw
Mar 24 at 9:23
@jazlaw, glad to hear this! Good luck with your research :)
– NM_
Mar 24 at 19:28
add a comment |
We can achieve this by recoding the 2 to 0 (i.e, 0 = "No") and using the following code with 2 functions used to determine whether a record satisfies criteria.
Please note that the code assumes that the race variables are numeric.
# Replicate your example
AWHITE = as.numeric(unlist(strsplit("1 1 1 1 1 1 2 2 2 1 1 2 1 1 1 1 1 1 1 1", " ")))
ABLACK = as.numeric(unlist(strsplit("2 1 2 2 2 2 1 1 1 2 2 1 2 2 2 2 2 2 2 2", " ")))
AASIAN = as.numeric(unlist(strsplit("2 2 2 1 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2", " ")))
AAMIND = as.numeric(unlist(strsplit("2 2 2 2 2 1 2 2 2 2 2 2 2 1 2 2 2 2 2 2", " ")))
APACI = as.numeric(unlist(strsplit("2 2 2 2 2 1 2 2 2 2 2 2 2 1 2 2 2 2 2 2", " ")))
nhes05v2 = data.frame(AWHITE, ABLACK, AASIAN, AAMIND, APACI)
> nhes05v2 # Partial output given
AWHITE ABLACK AASIAN AAMIND APACI
1 1 2 2 2 2
2 1 1 2 2 2
3 1 2 2 2 2
...
18 1 2 2 2 2
19 1 2 2 2 2
20 1 2 2 2 2
Recode the variables
# Recode variables. Change all 2's to 0's (New coding is 1 = Yes, 0 = No).
nhes05v2[nhes05v2 == 2] = 0
Create 2 functions to satisfy criteria for ARACE
and AORACE
# A person is mixed race it they answer 1 to more than one race
# Therefore a person whose row sum is greater than 1 is mixed race
determine.arace = function(AWHITE, ABLACK, AAMIND, AASIAN, APACI)
ifelse( sum(AWHITE, ABLACK, AAMIND, AASIAN, APACI ) > 1 , 91, 0)
determine.aothrace = function(AWHITE, ABLACK, AAMIND, AASIAN, APACI)
ifelse( sum(AWHITE, ABLACK, AAMIND, AASIAN, APACI ) > 1 , 2, 0)
Apply these functions to your data
ARACE = mapply(determine.arace, nhes05v2$AWHITE, nhes05v2$ABLACK, nhes05v2$AAMIND, nhes05v2$AASIAN, nhes05v2$APACI)
> ARACE
[1] 0 91 0 91 0 91 91 0 0 0 0 0 0 91 0 0 0 0 0 0
AOTHRACE = mapply(determine.aothrace, nhes05v2$AWHITE, nhes05v2$ABLACK, nhes05v2$AAMIND, nhes05v2$AASIAN, nhes05v2$APACI)
> AOTHRACE
[1] 0 2 0 2 0 2 2 0 0 0 0 0 0 2 0 0 0 0 0 0
To add them to your data frame
nhes05v2$ARACE = ARACE
nhes05v2$AOTHRACE = AOTHRACE
Thank you so much! This easily gets me to what I need for all that extra code! And thank you for your comments to help clarify my question.
– jazlaw
Mar 24 at 9:23
@jazlaw, glad to hear this! Good luck with your research :)
– NM_
Mar 24 at 19:28
add a comment |
We can achieve this by recoding the 2 to 0 (i.e, 0 = "No") and using the following code with 2 functions used to determine whether a record satisfies criteria.
Please note that the code assumes that the race variables are numeric.
# Replicate your example
AWHITE = as.numeric(unlist(strsplit("1 1 1 1 1 1 2 2 2 1 1 2 1 1 1 1 1 1 1 1", " ")))
ABLACK = as.numeric(unlist(strsplit("2 1 2 2 2 2 1 1 1 2 2 1 2 2 2 2 2 2 2 2", " ")))
AASIAN = as.numeric(unlist(strsplit("2 2 2 1 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2", " ")))
AAMIND = as.numeric(unlist(strsplit("2 2 2 2 2 1 2 2 2 2 2 2 2 1 2 2 2 2 2 2", " ")))
APACI = as.numeric(unlist(strsplit("2 2 2 2 2 1 2 2 2 2 2 2 2 1 2 2 2 2 2 2", " ")))
nhes05v2 = data.frame(AWHITE, ABLACK, AASIAN, AAMIND, APACI)
> nhes05v2 # Partial output given
AWHITE ABLACK AASIAN AAMIND APACI
1 1 2 2 2 2
2 1 1 2 2 2
3 1 2 2 2 2
...
18 1 2 2 2 2
19 1 2 2 2 2
20 1 2 2 2 2
Recode the variables
# Recode variables. Change all 2's to 0's (New coding is 1 = Yes, 0 = No).
nhes05v2[nhes05v2 == 2] = 0
Create 2 functions to satisfy criteria for ARACE
and AORACE
# A person is mixed race it they answer 1 to more than one race
# Therefore a person whose row sum is greater than 1 is mixed race
determine.arace = function(AWHITE, ABLACK, AAMIND, AASIAN, APACI)
ifelse( sum(AWHITE, ABLACK, AAMIND, AASIAN, APACI ) > 1 , 91, 0)
determine.aothrace = function(AWHITE, ABLACK, AAMIND, AASIAN, APACI)
ifelse( sum(AWHITE, ABLACK, AAMIND, AASIAN, APACI ) > 1 , 2, 0)
Apply these functions to your data
ARACE = mapply(determine.arace, nhes05v2$AWHITE, nhes05v2$ABLACK, nhes05v2$AAMIND, nhes05v2$AASIAN, nhes05v2$APACI)
> ARACE
[1] 0 91 0 91 0 91 91 0 0 0 0 0 0 91 0 0 0 0 0 0
AOTHRACE = mapply(determine.aothrace, nhes05v2$AWHITE, nhes05v2$ABLACK, nhes05v2$AAMIND, nhes05v2$AASIAN, nhes05v2$APACI)
> AOTHRACE
[1] 0 2 0 2 0 2 2 0 0 0 0 0 0 2 0 0 0 0 0 0
To add them to your data frame
nhes05v2$ARACE = ARACE
nhes05v2$AOTHRACE = AOTHRACE
We can achieve this by recoding the 2 to 0 (i.e, 0 = "No") and using the following code with 2 functions used to determine whether a record satisfies criteria.
Please note that the code assumes that the race variables are numeric.
# Replicate your example
AWHITE = as.numeric(unlist(strsplit("1 1 1 1 1 1 2 2 2 1 1 2 1 1 1 1 1 1 1 1", " ")))
ABLACK = as.numeric(unlist(strsplit("2 1 2 2 2 2 1 1 1 2 2 1 2 2 2 2 2 2 2 2", " ")))
AASIAN = as.numeric(unlist(strsplit("2 2 2 1 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2", " ")))
AAMIND = as.numeric(unlist(strsplit("2 2 2 2 2 1 2 2 2 2 2 2 2 1 2 2 2 2 2 2", " ")))
APACI = as.numeric(unlist(strsplit("2 2 2 2 2 1 2 2 2 2 2 2 2 1 2 2 2 2 2 2", " ")))
nhes05v2 = data.frame(AWHITE, ABLACK, AASIAN, AAMIND, APACI)
> nhes05v2 # Partial output given
AWHITE ABLACK AASIAN AAMIND APACI
1 1 2 2 2 2
2 1 1 2 2 2
3 1 2 2 2 2
...
18 1 2 2 2 2
19 1 2 2 2 2
20 1 2 2 2 2
Recode the variables
# Recode variables. Change all 2's to 0's (New coding is 1 = Yes, 0 = No).
nhes05v2[nhes05v2 == 2] = 0
Create 2 functions to satisfy criteria for ARACE
and AORACE
# A person is mixed race it they answer 1 to more than one race
# Therefore a person whose row sum is greater than 1 is mixed race
determine.arace = function(AWHITE, ABLACK, AAMIND, AASIAN, APACI)
ifelse( sum(AWHITE, ABLACK, AAMIND, AASIAN, APACI ) > 1 , 91, 0)
determine.aothrace = function(AWHITE, ABLACK, AAMIND, AASIAN, APACI)
ifelse( sum(AWHITE, ABLACK, AAMIND, AASIAN, APACI ) > 1 , 2, 0)
Apply these functions to your data
ARACE = mapply(determine.arace, nhes05v2$AWHITE, nhes05v2$ABLACK, nhes05v2$AAMIND, nhes05v2$AASIAN, nhes05v2$APACI)
> ARACE
[1] 0 91 0 91 0 91 91 0 0 0 0 0 0 91 0 0 0 0 0 0
AOTHRACE = mapply(determine.aothrace, nhes05v2$AWHITE, nhes05v2$ABLACK, nhes05v2$AAMIND, nhes05v2$AASIAN, nhes05v2$APACI)
> AOTHRACE
[1] 0 2 0 2 0 2 2 0 0 0 0 0 0 2 0 0 0 0 0 0
To add them to your data frame
nhes05v2$ARACE = ARACE
nhes05v2$AOTHRACE = AOTHRACE
edited Mar 24 at 18:21
answered Mar 23 at 17:08
NM_NM_
1,164320
1,164320
Thank you so much! This easily gets me to what I need for all that extra code! And thank you for your comments to help clarify my question.
– jazlaw
Mar 24 at 9:23
@jazlaw, glad to hear this! Good luck with your research :)
– NM_
Mar 24 at 19:28
add a comment |
Thank you so much! This easily gets me to what I need for all that extra code! And thank you for your comments to help clarify my question.
– jazlaw
Mar 24 at 9:23
@jazlaw, glad to hear this! Good luck with your research :)
– NM_
Mar 24 at 19:28
Thank you so much! This easily gets me to what I need for all that extra code! And thank you for your comments to help clarify my question.
– jazlaw
Mar 24 at 9:23
Thank you so much! This easily gets me to what I need for all that extra code! And thank you for your comments to help clarify my question.
– jazlaw
Mar 24 at 9:23
@jazlaw, glad to hear this! Good luck with your research :)
– NM_
Mar 24 at 19:28
@jazlaw, glad to hear this! Good luck with your research :)
– NM_
Mar 24 at 19:28
add a comment |
Using the dplyr
and magrittr
packages, my best version of this looks like:
nhes05v2 %>%
mutate(ARACE = ifelse(AWHITE == 1 & (ABLACK == 1 | AAMIND ==1 | AASIAN == 1 | APACI == 1), 91, 0),
AOTHRACE = ifelse(AWHITE == 1 & (ABLACK == 1 | AAMIND ==1 | AASIAN == 1 | APACI == 1), 2, 0))
The && conditional in R only checks against the first row, which is why you got the results you did here - here's a post from other people who have been flummoxed by that behavior.
add a comment |
Using the dplyr
and magrittr
packages, my best version of this looks like:
nhes05v2 %>%
mutate(ARACE = ifelse(AWHITE == 1 & (ABLACK == 1 | AAMIND ==1 | AASIAN == 1 | APACI == 1), 91, 0),
AOTHRACE = ifelse(AWHITE == 1 & (ABLACK == 1 | AAMIND ==1 | AASIAN == 1 | APACI == 1), 2, 0))
The && conditional in R only checks against the first row, which is why you got the results you did here - here's a post from other people who have been flummoxed by that behavior.
add a comment |
Using the dplyr
and magrittr
packages, my best version of this looks like:
nhes05v2 %>%
mutate(ARACE = ifelse(AWHITE == 1 & (ABLACK == 1 | AAMIND ==1 | AASIAN == 1 | APACI == 1), 91, 0),
AOTHRACE = ifelse(AWHITE == 1 & (ABLACK == 1 | AAMIND ==1 | AASIAN == 1 | APACI == 1), 2, 0))
The && conditional in R only checks against the first row, which is why you got the results you did here - here's a post from other people who have been flummoxed by that behavior.
Using the dplyr
and magrittr
packages, my best version of this looks like:
nhes05v2 %>%
mutate(ARACE = ifelse(AWHITE == 1 & (ABLACK == 1 | AAMIND ==1 | AASIAN == 1 | APACI == 1), 91, 0),
AOTHRACE = ifelse(AWHITE == 1 & (ABLACK == 1 | AAMIND ==1 | AASIAN == 1 | APACI == 1), 2, 0))
The && conditional in R only checks against the first row, which is why you got the results you did here - here's a post from other people who have been flummoxed by that behavior.
answered Mar 23 at 17:08
Michael MahoneyMichael Mahoney
1111
1111
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55315756%2fif-statement-with-multiple-conditions-always-saying-true-nested-in-while-loop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are your race variables (
AWHITE
,ABLACK
,AAMIND
, etc.) coded as numeric or factor levels?– NM_
Mar 23 at 16:18
Your code is not reproducible and testable. Please share a reproducible example of your data frame and the expected output. It is likely that we don't need any for-loop or while-loop to achieve your task.
– www
Mar 23 at 16:18
@NM_ they are currently numeric
– jazlaw
Mar 23 at 16:32
@www I've edited to add some context. Thank you both for your feedback!
– jazlaw
Mar 23 at 16:33
In order to belong to more than one race, is it possible for some one to be of two non-white races (for example, Asian and Black)? This is because your current if statement considers that a person is more than one race if they are white + at least one more race (i.e. in order to be mixed, that have to be white + some other race).
– NM_
Mar 23 at 16:48