0
$\begingroup$

So scrolling through my columns I find \N embedded. I need to count them, but I get an error. Would it be considered a missing value, it's new to me.

# Check whether attribute HeadOfState in country has any missing values, and if so, how many.
country$headOfState[country$headOfState==""] <- NA
country$headOfState[country$headOfState==\N] <- NA
sum(is.na(country$headOfState))

# Check whether attribute IndepYear in country has any missing values, and if so, how many.
country$IndepYear[country$indepYear==""] <- NA
country$indepYear[country$indepYear=='\N'] <- NA
sum(is.na(country$indepYear))

Error: unexpected input in "country$headOfState[country$headOfState==\"

country$headOfState[country$headOfState==\N] <- NA Error: unexpected input in "country$headOfState[country$headOfState==\"

$\endgroup$

1 Answer 1

1
$\begingroup$

The reason its shorws an error, bacause '\' is a part of base regex expressions in R. As states here:

The metacharacters in extended regular expressions are . \ | ( ) [ { ^ $ * + ?

So the comparisson like this

country$indepYear=='\N'

or this ( Its not a valid comparisson at all)

country$headOfState==\N 

will throw an error. You need to "escape" the "\" symbol. Try something like this instead if you want to replace "\N" with NA:

country$indepYear[country$indepYear=='\\N'] <- NA

If you need just to count them, you can use this approach:

sum(country$indepYear=='\\N') 

Hope this helps.

$\endgroup$
2
  • $\begingroup$ Thank you very much that definitely solved the problem. $\endgroup$
    – Chris Kehl
    Dec 5, 2018 at 12:42
  • $\begingroup$ @ChrisKehl you are welcome $\endgroup$ Dec 5, 2018 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.