I have a panel data set with two waves. I want to calculate the (possible) change from wave 1 to wave 2 of a selected variable and add the result as a new variable to the dataset. Two challenges: There are a considerable number of respondents who participated either only in the first wave or only in the second wave. Also, there are many NAs, which together make the use of lag or diff function difficult I think.
Example:
RespondentID <- c("id_001","id_002","id_002","id_003", "id_004", "id_004", "id_005", "id_005")
Wave <- c(1,1,2,2,1,2,1,2)
Value <- c(2,2,3,4,3,1,NA,3)
panel <- cbind(RespondentID,Wave,Value)
panel
RespondentID Wave Value
[1,] "id_001" "1" "2"
[2,] "id_002" "1" "2"
[3,] "id_002" "2" "3"
[4,] "id_003" "2" "4"
[5,] "id_004" "1" "3"
[6,] "id_004" "2" "1"
[7,] "id_005" "1" NA
[8,] "id_005" "2" "3"
The result should look like this:
RespondentID Wave Value Change
[1,] "id_001" "1" "2" NA
[2,] "id_002" "1" "2" NA
[3,] "id_002" "2" "3" "1"
[4,] "id_003" "2" "4" NA
[5,] "id_004" "1" "3" NA
[6,] "id_004" "2" "1" "-2"
[7,] "id_005" "1" NA NA
[8,] "id_005" "2" "3" NA