-1

This is what my data looks like on excel, where status and events are variables for the mentioned dates. Events columns don't have data.

30/12/2018     31/12/2018           01/01/2019  
Status  event      status   event   status  event

delivered      ncp              ncp 

ncp        delivered            ncp     

I want to make these variables my headings and append these dates as a column.

Note: This is just a sample of my data, in reality, dates are available for a month. Please help.

s_baldur
  • 29,441
  • 4
  • 36
  • 69

2 Answers2

0

Save your excel file as CSV. Then use R read.csv() function

sample_data <- read.csv(file="c:/mySampleData.csv", header=TRUE, sep=",")

The first parameter of the function is path to your file.
It converts your data into a data frame and you can access the columns using $, e.g. sample_data$Status will return the status column.

PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
0

Not sure if this is the kind of thing you're looking for, but this should put the data into a good structured format.

library(readxl)
library(dplyr)
library(tidyr)

file <- # INSERT FILE PATH HERE

df <- read_excel(file, col_names = FALSE)

df2 <- df %>%
       mutate(SEQ_NO = ifelse(row_number() > 2, row_number() - 2, NA)) %>%
       gather(COL, VAL, -SEQ_NO) %>%
       mutate(DATE = ifelse(grepl('/', VAL), VAL, NA),
              DATE = as.Date(DATE, format = '%d/%m/%Y')) %>%
       fill(DATE) %>%
       mutate(KEY = ifelse(VAL %in% c('status', 'event'), toupper(VAL), NA)) %>%
       fill(KEY) %>%
       filter(!is.na(SEQ_NO)) %>%
       select(-COL) %>%
       spread(KEY, VAL) %>%
       arrange(DATE, SEQ_NO) %>%
       select(DATE, SEQ_NO, STATUS, EVENT)
Matt Jewett
  • 3,249
  • 1
  • 14
  • 21