I have a long data frame having 96 consecutive observation belonging to same variable. I have to two such variables. One variable is Energy denoted by "param2(kWh)" and another is demand denoted by "param2(kW)". So data frame is like---

first row has variables described as "Interval" and "param2(kWh)" and then 96 observations belonging to Energy variable, followed by 98th row having variables described a "Interval" and "param2(kW)" and then 96 observations belonging to Demand variable.

Again after that there is row described as "Interval" and "param2(kWh)" and then 96 observations belonging to Energy variable. After that again there is row describing variables "Interval" and "param2(kW)" and 96 observations belonging to Demand variable and such intertwining of these two variables i.e. Energy and Demand continues for long.

The objective is to have two separate data frames one for Energy i.e. "param2(kWh)" and one for Demand i.e. "param2(kW)". How these two data frames be formed from the above single data frame?

I am attaching the internet site of the data frame.


  • $\begingroup$ What have you tried? You shall show data in a form so that readers can copy/paste. $\endgroup$
    – tagoma
    Aug 6 '17 at 5:35
  • $\begingroup$ It is a huge excel file. Can you tell me how can I make that file available to readers? $\endgroup$
    – Manik
    Aug 6 '17 at 8:01
  • $\begingroup$ Upload your file somewhere on the internet, or paste a small part here but not as an image. $\endgroup$
    – tagoma
    Aug 6 '17 at 9:55
  • $\begingroup$ Can you give a sample data with n rows for param2(kWh) and then n rows for param2(kW) as well as sample values for interval. n = 2 should work but more is welcome too. Also using the same sample data, also what your expectation is. I can't access One drive from my worksite. $\endgroup$
    – Drj
    Aug 10 '17 at 15:18

Here is one way to generate two data frames based on the data.

# read data
dat <- read.csv("sample csv1.csv", stringsAsFactors = FALSE, header = FALSE)

# find rows containing "Interval"
idx_int <- dat[[1]] == "Interval"

# calculate groups
group <- cumsum(idx_int) %% 2

# remove indices for "Interval" rows
group2 <- group[!idx_int]

# split data frame into two data frames
dat_list <- split(dat[!idx_int, ], group2)

# generate two separate data frames
dat_energy <- dat_list[[2]]  # Energy
dat_demand <- dat_list[[1]]  # Demand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.