An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20140215/27497968/attachment.pl>
Aligning names with variable values
4 messages · arun, Barry King
May be this helps:
Prices.df <- read.table(text="rowID? Price
1 'Full Season' 417.95
2 'Full Season' 679.43
3 'Full Season' 839.79
4 'Half Season' 159.39
5 'Half Season' 256.93",sep="",header=TRUE,stringsAsFactors=FALSE)
upperLimit <- c(`Full Season`=779.12, `Half Season`=231.11)
rejectdf <- do.call(rbind,lapply(names(upperLimit),function(x) {x1 <- subset(Prices.df,rowID==x); indx <- with(x1,cut(Price,breaks=c(-Inf,upperLimit[x],Inf),labels=FALSE)); x1[indx>1,]}))
acceptdf <- do.call(rbind,lapply(names(upperLimit),function(x) {x1 <- subset(Prices.df,rowID==x); indx <- with(x1,cut(Price,breaks=c(-Inf,upperLimit[x],Inf),labels=FALSE)); x1[indx==1,]}))
A.K.
On Saturday, February 15, 2014 4:36 AM, Barry King <barry.king at qlx.com> wrote:
I wish to compare prices in a dataframe to calculated upper limits and then reject (collect in a reject dataframe) those rows with prices that are above the upper limit but I am having trouble aligning the variable value in the Prices dataframe with the names in the upper limit file.
upperLimit
? Full Season? ? Half Season ? ? ? 779.12? ? ? ? ? 231.11
Prices.df
? ? ? ? rowID? Price 1 Full Season 417.95 2 Full Season 679.43 3 Full Season 839.79 4 Half Season 159.39 5 Half Season 256.93 Any assistance you can provide is greatly appreciated. Barry King ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Also, you could try: library(plyr) ?indx <- ddply(Prices.df,.(rowID),summarise, !findInterval(Price,unique(upperLimit[match(rowID,names(upperLimit))])))[,2] Prices.df[indx,] ?Prices.df[!indx,] #or indx1 <- !with(Prices.df,ave(seq_along(Price),rowID,FUN=function(x) findInterval(Price[x],unique(upperLimit[match(rowID[x],names(upperLimit))])))) Prices.df[indx1,] ?Prices.df[!indx1,] A.K.
On Saturday, February 15, 2014 12:19 PM, arun <smartpink111 at yahoo.com> wrote:
May be this helps:
Prices.df <- read.table(text="rowID? Price
1 'Full Season' 417.95
2 'Full Season' 679.43
3 'Full Season' 839.79
4 'Half Season' 159.39
5 'Half Season' 256.93",sep="",header=TRUE,stringsAsFactors=FALSE)
upperLimit <- c(`Full Season`=779.12, `Half Season`=231.11)
rejectdf <- do.call(rbind,lapply(names(upperLimit),function(x) {x1 <- subset(Prices.df,rowID==x); indx <- with(x1,cut(Price,breaks=c(-Inf,upperLimit[x],Inf),labels=FALSE)); x1[indx>1,]}))
acceptdf <- do.call(rbind,lapply(names(upperLimit),function(x) {x1 <- subset(Prices.df,rowID==x); indx <- with(x1,cut(Price,breaks=c(-Inf,upperLimit[x],Inf),labels=FALSE)); x1[indx==1,]}))
A.K.
On Saturday, February 15, 2014 4:36 AM, Barry King <barry.king at qlx.com> wrote:
I wish to compare prices in a dataframe to calculated upper limits and then reject (collect in a reject dataframe) those rows with prices that are above the upper limit but I am having trouble aligning the variable value in the Prices dataframe with the names in the upper limit file.
upperLimit
? Full Season? ?? Half Season ? ? ?? 779.12? ? ? ? ? 231.11
Prices.df
? ? ? ? rowID? Price 1 Full Season 417.95 2 Full Season 679.43 3 Full Season 839.79 4 Half Season 159.39 5 Half Season 256.93 Any assistance you can provide is greatly appreciated. Barry King ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20140215/a8e0e6e7/attachment.pl>