I am trying to reproduce results from Stata in R. The specific line that is giving me trouble seems simple.
The line of Stata is
sum peace_index_score if Africa == 1
The code I tried in R is
if (World$Africa == 1){
summary(World$peace_index_score)
}
This returns the following error message:
Warning message in if (World$Africa == 1) {:
“the condition has length > 1 and only the first element will be used”
CodePudding user response:
It would be
summary(World$peace_index_score[World$Africa == 1])
CodePudding user response:
Stata automatically applies the test if Africa == 1
observation by observation (row by row, if you like) given syntax like
sum peace_index_score if Africa == 1
This is an example of an if
qualifier.
Stata is like R to the extent that use of the if
command
if Africa == 1 sum peace_index_score
is interpreted as
if Africa[1] == 1 sum peace_index_score
However, that construct in Stata is legal, just rarely what you want, as almost always the if
command is best used for comparing string or numeric constants (and in fact the above command is doing exactly that). So, it would be legal and idiomatic in Stata to check first for any values of Africa
that are 1.
count if Africa == 1
if r(N) == 0 sum peace_index if Africa == 1