Why does NaN^0 == 1

This is referenced in the help page referenced by ?’NaN’ “The IEC 60559 standard, also known as the ANSI/IEEE 754 Floating-Point Standard. http://en.wikipedia.org/wiki/NaN.” And there you find this statement regarding what should create a NaN: “There are three kinds of operations that can return NaN:[5] Operations with a NaN as at least one operand. It … Read more

Select last non-NA value in a row, by row

Do this by combining three things: Identify NA values with is.na Find the last value in a vector with tail Use apply to apply this function to each row in the data.frame The code: lastValue <- function(x) tail(x[!is.na(x)], 1) apply(df, 1, lastValue) [1] 2 6 3 4 1

replace NA value with the group value

Try ave. It applies a function to groups. Have a look at ?ave for details, e.g.: df$med_card_new <- ave(df$med_card, df$hhold_no, FUN=function(x)unique(x[!is.na(x)])) # person_id hhold_no med_card med_card_new #1 1 1 1 1 #2 2 1 1 1 #3 3 1 NA 1 #4 4 1 NA 1 #5 5 1 NA 1 #6 6 2 0 … Read more