-1

I need a for loop to sum the values in a DataFrame column in r. The code below gives me the sum of the value when the date column == '2014-01-01'. I also need when date == '2014-01-02' to '2014-31-12'

# sum of the half an hour data into daily total.
day1 <- subset(df, DATE =='2014-01-01',select= FridgeRange)
sum(day1)

Please if you can also add the result to a dataframe that will be awesome.

mhovd
  • 2,814
  • 2
  • 16
  • 35

2 Answers2

0

library(dplyr)

dat = read.csv("Your_file")

dateinbw = c("2014-01-01", "2014-01-02")

new_dat = filter(dat, DATE %in% dateinbw)

sum(new_dat$your_column)

0

This loop will cycle through the dates from 2014-01-01 to 2014-12-31. It's not exactly clear to me how you want to name the extracted variable and where to put it, so I will leave it to you.

date <- as.Date("2014-01-01") 

for (i in 1:364) {
     date <- date+1
     print(toString(date))
     x <- sum(subset(df, DATE==toString(date), select= FridgeRange))
     # now do something with x
     }
Mario Niepel
  • 1,017
  • 2
  • 16