How to append results as a matrix to a dataframe using functions in R -


i working these 2 dataframes

datetime <- c("2016-01-20 08:30:13", "2016-01-20 12:45:00", "2016-01-20 02:53:20", "2016-01-20 03:22:18", "2016-01-20 21:42:10",                "2016-01-21 07:55:50", "2016-01-21 13:14:10", "2016-01-21 15:42:16", "2016-01-21 18:31:15", "2016-01-21 19:13:10") measurement <- c(10,120,180,30,240,40,300,380,960,390) outlier <- c("false","true","true","false","true","false","true","true","true","true") type <- c("length","length","height","breadth","length",           "breadth","breadth","height","height","length" ) df <- data.frame(datetime,measurement,outlier,type)     df$datetime <- as.posixct(df$datetime,format="%y-%m-%d %h:%m:%s")   start <- c("2016-01-20 00:00:00","2016-01-21 00:00:00") end <- c("2016-01-20 23:59:59","2016-01-21 23:59:59") days <- c("day1","day2")     df2 <- data.frame(start,end,days) 

i trying breakdown of count of outliers type. trying way.

df_final <- data.frame(apply(df2, 1, function(d) {   df3 <- subset(df, datetime >= as.posixct(d["start"]) & datetime <= as.posixct(d["end"]))   df3_outliers <- subset(df3, outlier=="true")   df3_outliers$days <- d["days"]    df_breakdown <- dcast(df3_outliers,days ~ type,value.var="days")  })) 

this gives me

  days height length days.1 breadth height.1 length.1   day1      1      2   day2       1        2        1 

but desired output

  days breadth height length       day1      0    1      2    day2      1    2      1 

i know missing basic matrix using apply function not sure going wrong

kindly provide inputs , directions on solving this.

edit:

while david's answered worked above solution in tricky situation here while applying david's logic bigger dataset.

here explanation: (which didn't earlier) firstly, apologize putting both data day1 , day2 in df. df has 2 chunks of data (day1 , day2) doesn't exist above. df3 data looks after run outlier analysis day1 means won't able see data day2 (the last 5 rows in df) unless program completes outlier analysis day1. reason have used df3 subset dataframe.

              datetime measurement outlier    type 1  2016-01-20 08:30:13          10   false  length 2  2016-01-20 12:45:00         120    true  length 3  2016-01-20 02:53:20         180    true  height 4  2016-01-20 03:22:18          30   false breadth 5  2016-01-20 21:42:10         240    true  length 

i have breakdown type

      days breadth height length           day1      0    1      2  

i need store information in dataframe , run outlier program day 2 , df3 have data day2. after breakdown day2, append result day1 final data frame.

i hope clear. please let me know if isn't.

this sort of loop keeps appending breakdowns day after every run , obtain dataframe "desired output".

you looking overlapping joins seems, here's possible data.table::foverlaps solution (i'm assuming start , end of posixct class in real life, rather factors)

library(data.table) setkey(setdt(df)[, c("start", "end") := datetime], start, end) dcast(foverlaps(setdt(df2), df[outlier == "true"], nomatch = 0l), days ~ type) #    days breadth height length # 1: day1       0      1      2 # 2: day2       1      2      1 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -