r - conditional sum with dates in column names -


want calculate conditional sum based on specified dates in r. sample df

start_date = c("7/24/2017", "7/1/2017", "7/25/2017") end_date   = c("7/27/2017", "7/4/2017", "7/28/2017") `7/23/2017` = c(1,5,1) `7/24/2017` = c(2,0,2) `7/25/2017` = c(0,0,10) `7/26/2017` = c(2,2,2) `7/27/2017` = c(0,0,0) df = data.frame(start_date,end_date,`7/23/2017`,`7/24/2017`,`7/25/2017`,`7/26/2017`,`7/27/2017`) 

in excel looks like:

enter image description here

i want perform calculations specified in column h conditional sum of columns c through g based on dates specified in columns , b.

apparently, excel allows columns dates not r.

you achieve follows:

# number of trailing columns without numeric values c = 2  # create separate vector dates dates = as.date(gsub("x","",tail(colnames(df),-c)),format="%m.%d.%y")  # convert date columns in dataframe df$start_date = as.date(df$start_date,format="%m/%d/%y") df$end_date = as.date(df$end_date,format="%m/%d/%y")  # calculate sum sapply(1:nrow(df),function(x) {y = df[x,(c+1):ncol(df)][dates %in%    seq(df$start_date[x],df$end_date[x],by="day")  ]; ifelse(length(y)>0,sum(y),0) }) 

returns:

[1]  4  0 12 

hope helps!


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -