0

I have a dataframe like this

  X  2001,2002,2003
 JAN   NA,1,2
 JUN   NA,2,3
 DEC   1,2,NA

I want an empty vector to store values and generate a time series What can I do

Intended output formated by month and year, omit NAs

 output=c(1,1,2,2,2,3)

How can I do?

1
  • If rows and columns were in desired order, you could try: as.vector(na.omit(unlist(df[-1]))) Commented Feb 23, 2020 at 3:30

1 Answer 1

1

You might go that direction:

library(tidyverse)

dta <- tribble(
  ~X,    ~"2001", ~"2002", ~"2003",
  "JAN", NA,      1,       2,
  "JUN", NA,      2,       3,
  "DEC", 1,       2,       NA)

dta %>%
  pivot_longer(cols = '2001':'2003', 
               names_to = "year", 
               values_to = "val") %>%
  arrange(year) %>%
  filter(!is.na(val))

However, you need to assure that the months are sorted correctly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.