I am writing a function so that I can count the number of NAs in the columns I am interested in. Below, pts_au
is a balanced panel data set of country-years, PTS_A
is the column name, and the Country.string
is the column I want to condition on.
> if (pts_au$Country.string == "China"){
+ sum(is.na(pts_au$PTS_A))
+ }
Warning message:
In if (pts_au$Country.string == "China") { :
the condition has length > 1 and only the first element will be used
Basically I am trying to count the number of NAs in the PTS_A
column, when the country name is China
.
> unique(pts_au$Country.string)
[1] "Armenia" "Azerbaijan" "Belarus" "China"
[5] "Kazakhstan" "Russia" "Tunisia" "Uganda"
> str(pts_au$Country.string)
chr [1:112] "Armenia" "Armenia" "Armenia" "Armenia" ...
What would the error mean? Should I subset the dataframe first and then apply is.na
?