BLACK FRIDAY EXTRA SAVINGS EVENT - EXTENDED
START FREE

I require some help regarding understanding R syntax

Screen Link:

The answer:

for (col in location_cols ) {
    mvc[is.na(mvc[col]),col] <- sup_data[is.na(mvc[col]),col]
}

Query:
I am not sure what does "mvc[is.na(mvc[col]),col] " do. Could someone please explain?
Thank you! :slight_smile:

Hello @lauratangwt, I hope you are doing well. In R data.frame you can index row and column as this df_name[rows, columns] to select an item in a dataframe. The is.na function checks if an element is a Null object.


We can use a boolean object when indexing a DataFrame, that is why they used is.na(mvc[col]) to get the rows in the col column containing NULL.

Consider this Example:

> df = data.frame(a=c(NA, 2,3,4), b=c(5, 7, 6, 8), c=c(7,9,10,7))
> df
   a b  c
1 NA 5  7
2  2 7  9
3  3 6 10
4  4 8  7

Let’s Check Rows with Null values

> df[is.na(df$a),]
   a b c
1 NA 5 7
1 Like