Hi,
I need to make individual datasets containing date and one variable from the entire dataset. Each variable has some NAs and we need to remove these NAs as well while retaining non-NA values. I am doing it variable by variable as of now. But if the number of variables become more, then this method is not efficient. Is there a way to do the same with all variables at the same time. Below is a small example to get the idea of what I am looking for.
x <- data.frame(
date = c("2020-05-01",
"2020-06-01","2020-07-01",
"2020-08-01","2020-09-01","2020-10-01",
"2020-11-01","2020-12-01",
"2021-01-01","2021-02-01",
"2021-03-01","2021-04-01"),
A = c(5081800.82485142,
4977717.33466686,4492457.276,
4498059.839,NA,NA,NA,NA,NA,
NA,NA,NA),
B = c(146.229784586414,
151.929662325412,151,146,144,
145,NA,NA,NA,NA,NA,NA),
C = c(48.211004603533,
48.211004603533,40.682499,
42.390106,39.560101,38.33,39.22,
39.5,39.89,40.28,40.65,40.98),
D = c(623692.818482367,
631337.814039963,608986.5,
621308.4,NA,NA,NA,NA,NA,NA,NA,
NA)
)
# Currently creating A, B, C, and D one by one.
A <- x%>%
select(date, A)%>%
drop_na(A)
B <- x%>%
select(date, B)%>%
drop_na(B)
D <- x%>%
select(date, D)%>%
drop_na(D)
I would like to be able to achieve individual data sets A, B, C and D at once while also removing NAs from each of them.
Thanks for your help!