I have the following problem. I have a data frame that looks like this:
Date Period Subject Skill
1.1.2020 1 1 1
1.1.2020 1 2 3
1.1.2020 1 3 1
1.1.2020 1 4 3
1.1.2020 2 1 NA
1.1.2020 2 2 NA
1.1.2020 2 3 NA
1.1.2020 2 4 NA
1.1.2020 3 1 NA
1.1.2020 3 2 NA
1.1.2020 3 3 NA
1.1.2020 3 4 NA
1.1.2021 1 1 4
1.1.2021 1 2 4
1.1.2021 1 3 1
1.1.2021 1 4 3
1.1.2021 2 1 NA
1.1.2021 2 2 NA
1.1.2021 2 3 NA
1.1.2021 2 4 NA
1.1.2021 3 1 NA
1.1.2021 3 2 NA
1.1.2021 3 3 NA
1.1.2021 3 4 NA
I would like to copy the Skill
value from Period 1 for each subject that took the test on the same date (in this case representing a unique group) to the same subject in Period 2 and Period 3. The desired output should look like this:
Date Period Subject Skill
1.1.2020 1 1 1
1.1.2020 1 2 3
1.1.2020 1 3 1
1.1.2020 1 4 3
1.1.2020 2 1 1
1.1.2020 2 2 3
1.1.2020 2 3 1
1.1.2020 2 4 3
1.1.2020 3 1 1
1.1.2020 3 2 3
1.1.2020 3 3 1
1.1.2020 3 4 3
1.1.2021 1 1 4
1.1.2021 1 2 4
1.1.2021 1 3 1
1.1.2021 1 4 3
1.1.2021 2 1 4
1.1.2021 2 2 4
1.1.2021 2 3 1
1.1.2021 2 4 3
1.1.2021 3 1 4
1.1.2021 3 2 4
1.1.2021 3 3 1
1.1.2021 3 4 3
I saw a similar thread How to copy value of a cell to other rows based on the value of other two columns? however there is just one missing NA value per group (and not many NAs per group). With the following solution from zoo
package I am only replacing each NA with the most recent non-NA prior to it.
x %>% group_by(Date, Subject) %>% mutate(Skill= na.locf(Skill))
In short, I want to match the value of a cell to other rows based on the same Subject ID within a group.
Thanks for all your answers!