r - Filtering on duplicated attributes -
i have dataframe of widgets want created predictions:
mydata<-data.frame(widget=c(1,2,3,4,5), type=c("a","e","c","c","d"))
the way model works create type a:e every widget , predict. each prediction assigned attribute state model used generate prediction:
alltypes<-merge(mydata[,-which(names(mydata)=="type")], data.frame(type = c("a","b","c","d","e"), = true)) names(alltypes)[1]<-"widget" alltypes$predictions<-5*1:25 attr(alltypes$predictions,"modelused")<- c(rep("model1",5),rep("model2",5),rep("model1",5),rep("model3",5),rep("model2",5 ))
i want return predictions jsutfor original type in mydata join on original table:
library(dplyr) finaldata<- inner_join(alltypes, mydata, = c("widget", "type"))
an examination of attributes finaldata shows there 25 attributes retured.
attr(alltypes$predictions,"modelused")
can suggest method returning 5 attributes relevant predictions returned finaldata, or method filtering on attributes can re-add correct attributes post join?
ah works:
attr(alltypes$predictions,"modelused")[which(alltypes$type=="a")]
or better still
originalrows<-alltypes$widget==mydata$widget & as.character(alltypes$type)==as.character(mydata$type) mydata<-mydata[order(mydata$type,mydata$widget),] attr(alltypes$predictions,"modelused")[which(originalrows)]
Comments
Post a Comment