Interview Question: Subtle Problem

Sample Question #123 (programming – R)

What’s wrong with the following R code segment? Here "mm" is an N x K dataset that’s already read into memory and "prc" is one of the variables in "mm".

ss=mm[2:59,]
av=mean(ss$prc)
if (av >= 50) { pg = "large" }

[Question courtesy of Dr. Charles Qin of ITG]

Advertisements
This entry was posted in Sample Qs. Bookmark the permalink.

One Response to Interview Question: Subtle Problem

  1. Brett says:

    ANSWER
     
    There’s nothing wrong with the syntax itself. The problem arises if the last row number of mm is less than 59. Say mm has only 30 rows. Then ss will have values from mm in the first 30 rows, but R will fill the rest of ss with missing values.  This means av will evaluate to missing (unlike in, say, SAS).  Then the if statement will fail as R cannot evaluate the boolean.
     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s