I have an odd problem. Look at df
:
Year <- c(1994, rep("NA", 11),1995,rep("NA",11))
Month <-c(1:12, 1:12)
df <- data.frame(Year,Month)
The year appears every 12 lines, but I want on all lines, like:
Year <- c(rep(1994, 12), rep(1995,12))
Month <-c(1:12, 1:12)
df_2 <- data.frame(Year,Month)
> df_2
Year Month
1 1994 1
2 1994 2
3 1994 3
4 1994 4
5 1994 5
6 1994 6
7 1994 7
8 1994 8
9 1994 9
10 1994 10
11 1994 11
12 1994 12
13 1995 1
14 1995 2
15 1995 3
16 1995 4
17 1995 5
18 1995 6
19 1995 7
20 1995 8
21 1995 9
22 1995 10
23 1995 11
24 1995 12
I was trying a loop approach, but I wasn't able to solve this.
PS: this df
goes until 2020, so the Month column goes until 11 in 2020.
I appreciate it if someone can help :)
We can have NA
without the quotes, and use fill
library(dplyr)
library(tidyr)
df %>%
mutate(Year = na_if(Year, "NA")) %>%
fill(Year)
-output
# Year Month
#1 1994 1
#2 1994 2
#3 1994 3
#4 1994 4
#5 1994 5
#6 1994 6
#7 1994 7
#8 1994 8
#9 1994 9
#10 1994 10
#11 1994 11
#12 1994 12
#13 1995 1
#14 1995 2
#15 1995 3
#16 1995 4
#17 1995 5
#18 1995 6
#19 1995 7
#20 1995 8
#21 1995 9
#22 1995 10
#23 1995 11
#24 1995 12