63537

Operation on multiple zoo object with data frame

Question:

I have two time series (zoo) objects and a data frame<br /> z1

z1 <- structure(c(400L, 125L, 125L, 125L, 120L,400L, 125L, 125L, 125L, 120L,400L, 125L, 125L, 125L, 120L ,400L, 125L, 125L, 125L, 120L), .Dim = c(5L, 4L), .Dimnames = list(NULL, c("T1", "T2", "T3", "T6" )), index = structure(c(15723, 15725, 15726, 15727, 15728), class = "Date"), class = "zoo") T1 T2 T3 T6 2013-01-18 400 400 400 400 2013-01-20 125 125 125 125 2013-01-21 125 125 125 125 2013-01-22 125 125 125 125 2013-01-23 120 120 120 120

z2

z2 <- structure(c(40L, 12L, 25L, 15L, 10L,40L, 25L, 15L, 123L, 190L,150L, 115L, 155L, 105L, 80L ,40L, 425L, 225L, 115L, 20L), .Dim = c(5L, 4L), .Dimnames = list(NULL, c("T1", "T2", "T3", "T6" )), index = structure(c(15723, 15725, 15726, 15727, 15728), class = "Date"), class = "zoo") T1 T2 T3 T6 2013-01-18 40 40 150 40 2013-01-20 12 25 115 425 2013-01-21 25 15 155 225 2013-01-22 15 123 105 115 2013-01-23 10 190 80 20

df

l <- "Name, DOB, TypeOfApply, House T1, 2008-12-16, sync,44 T2, 2008-12-15, sync,54 T3, 2008-12-19, async,34 T4, 2008-12-18, async,84 T5, 2008-12-11, sync,94" df <- read.csv(text = l)

I want to apply a formula(function I created to use "calc") bsaed on condition that TypeOfApply == "sync". Z1 and Z2 is going to have same no of rows and columns.

calc(z1,z2,df$DOB-2013-01-18,df$House) T1 T2 T3 T6 2013-01-18 calc(400,40,((2008-12-16)-(2013-01-18)),44) calc(400,40,((2008-12-15)-(2013-01-18)),54) 400 400 2013-01-20 calc(125,12,((2008-12-16)-(2013-01-20)),44) calc(400,25,((2008-12-15)-(2013-01-20)),54) 125 125 2013-01-21 calc(125,25,((2008-12-16)-(2013-01-21)),44) calc(400,15,((2008-12-15)-(2013-01-21)),54) 125 125 2013-01-22 calc(125,15,((2008-12-16)-(2013-01-22)),44) calc(400,123,((2008-12-15)-(2013-01-22)),54) 125 125 2013-01-23 calc(120,10,((2008-12-16)-(2013-01-23)),44) calc(400,190,((2008-12-15)-(2013-01-23)),54) 120 120

So, in this code T1 and T2 will have formula to be applied, but others will not<br /> T3 - Type of Apply is async<br /> T5 - Does not exist in z1 and z2<br /> T6 - Does not exist in df

<strong>Update</strong><br /> Sequence of names in df may be different. So it may be like T2, T1, T3, T5, T4

Just as sample calc function

calc <- function(x,y,z,v) { val <- x+y+(z/365)+v return(val) }

Answer1:

Here, I am using str_trim as there are leading/lagging spaces in "df" columns. Converted the "factor" column "DOB' to "Date" class, created a "indx" based on the condition that of "TypeOfApply" elements are "sync" and corresponding "Name" elements are present in the column names of "z1". This "indx" is used for subsetting the "df", as well as "z1", and "z2". Then use "Map" function and get the corresponding columns of "z1", "z2", elements of "df1$DOB", "df1$House", which can be used as inputs in the "calc" function.

library(stringr) indx <- intersect(with(df,str_trim(Name[str_trim(TypeOfApply)=='sync'])), colnames(z1)) df1 <- df[str_trim(as.character(df$Name)) %in% indx,c(2,4)] df1$DOB <- as.Date(str_trim(df1$DOB)) Map(function(u,v,x,y) calc(u,v, x-'2013-01-18', y), as.data.frame(z1[,indx]), as.data.frame(z2[,indx]), df1$DOB, df1$House) <h3>Update</h3>

Using the calc function from OP's post

z3 <- z1[,indx] index <- as.Date('2013-01-18') z3[] <- mapply(calc, as.data.frame(z1[,indx]), as.data.frame(z2[,indx]), df1$DOB-index, df1$House) z3 # T1 T2 #2013-01-18 479.9068 489.9041 #2013-01-20 176.9068 199.9041 #2013-01-21 189.9068 189.9041 #2013-01-22 179.9068 297.9041 #2013-01-23 169.9068 359.9041

Suppose, if I change the order of "df" rows

set.seed(24) df <- df[sample(1:nrow(df)),]

Then, the "Map" list elements will be in the same order as "indx", for example,

indx #[1] "T2" "T1" df1 # DOB House #2 2008-12-15 54 #1 2008-12-16 44 Map(function(u,v,x,y) u, as.data.frame(z1[,indx]), as.data.frame(z2[,indx]), df1$DOB, df1$House) #$T2 #[1] 400 125 125 125 120 #$T1 #[1] 400 125 125 125 120

Recommend

  • Error connecting to Zookeeper with Spark Streaming Structured
  • MySQL query giving erroneous results because of the mode of only full group by [duplicate]
  • Paypal payment page layout broken in webview android maybe css not loaded correctly?
  • LINQ error: The null value cannot be assigned to a member with type System.Int32 which is a non-null
  • How to update Resource value in ReadyAPI/SoapUI dynamically by groovy?
  • How to use case/esac in process substitution?
  • Excel changes conditional formatting formula
  • Find and copy code
  • Dynamically load images from project folder - Windows Phone 7
  • Remove parenthesis and text in a file using Python
  • How to delete column from range if cell contains specific value in VBA/Excel
  • data.table replicate rows after join?
  • How to remove comma or any characters from Python dataframe column name
  • How to remove left and right margins from all wrapped rows in flexbox (without nth-child or js)
  • What exactly is the height of modalPresentationStyle - FormSheet on iPad?
  • SSH in Bash Script Messing Up File Read
  • calculating number of bytes of each row in an image
  • Excel distinct count with conditions
  • is there a way to update filter with async data
  • Authentication failed with Azure Active Directory in Windows Phone
  • In loopback documentation what does variable 'cb' stands for?
  • ThreadStatic in asynchronous ASP.NET Web API
  • Not able to aggregate on nested fields in elasticsearch
  • Reduction and collapse clauses in OMP have some confusing points
  • Textfile Structure (tables)
  • MongoDb aggregation
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Incrementing object id automatically JS constructor (static method and variable)
  • MySQL WHERE-condition in procedure ignored
  • Web-crawler for facebook in python
  • WPF Applying a trigger on binding failure
  • trying to dynamically update Highchart column chart but series undefined
  • Free memory of cv::Mat loaded using FileStorage API
  • unknown Exception android
  • Can't mass-assign protected attributes when import data from csv file
  • Binding checkboxes to object values in AngularJs
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?