[R] Data manipulation

Petr PIKAL petr.pikal at precheza.cz
Mon Oct 15 16:01:36 CEST 2007


Hi

r-help-bounces at r-project.org napsal dne 15.10.2007 14:36:59:

> 2007/10/15, Stephen Tucker <brown_emu at yahoo.com>:
> >
> > Hi Klaus,
> >
> > I am not exactly sure what you are asking for, but something like 
this?
> > This
> > would be option (2) from your list - I don't know that it would be too
> > difficult in R that you would want to use another tool.
> >
> > filt <- function(x)
> >   with(x,which(Hole > 1))
> >
> > normalize <- function(x,y)
> > {
> >   x$Feature <- x$Feature - with(y,Nominel[match(x$Hole,Hole)])
> >   return(x)
> > }
> >
> > # from CSV import
> > mydata <- read.csv(...)
> >
> > # answer
> > normalizedData <- normalize(filt(mydata))
> 
> 
> I have not tried your solution yet, I was maybe more looking for 
somewhat
> like this
> 
> rawdate$nomalized <- rawdata$Value - nom$Nominel[nom$Hole == 
rawdata$Hole &&
> nom$Feature == rawdata$Feature]

Well, let us put it another way. Do you want for each unique combination 
of Hole and Feature to subtract a value from one data frame, let us call 
it nom from another data frame, let us call it raw? If yes I assume you 
have exactly same number of unique combinations of Hole and Feature values 
in both datasets.

In that case you can use such for cycle (although there can be other finer 
solutions)

for (i in 1:no.of.unique.levels) {
selection <- interaction(raw$hole, raw$feature) %in% interaction(nom$hole, 
nom$feature)[1]
raw[selection,"value"] <- raw[selection,"value"]-nom[i,"value"]
}






> 
> maybe it should be like this
> 
> nominel.value <- function(hole, feature, nominel.array)
> {
>  n <- nominel.array$nominel[nominel.array$Hole == hole &
>                                          nominel.array$Feature == 
feature]
>  return(n)
> }
> 
> rawdata$normalised <- rawdata$Value - nominel.value(rawdata$Hole,
> rawdata$Feature, nom)
> 
> But it does not seem to work, the problem is that I expect nominel.value 
to
> return a single value but it returns a vector, but not as expected.
> 
> I think that adding a new column to the data frame is better.
> 
> The rawdata is actually hole position and radius (x,y, r), Hole is the 
index
> number of a hole on a singel sample, and I have measured on 98 different
> samples. As a part of a process capability analysis I would like to 
compare
> the measured values for each holes by subtracting the target/should be/
> nominel value, so i look at deviation from the "true" value. I hope this
> explains this better.
> 
> 
> /Klaus
> 
> 
> 
> 
> 
> 
> --- Klaus Friis Řstergaard <farremosen at gmail.com> wrote:
> >
> > > Hi,
> > >
> > > I have a data set which is like this I write as the CSV I import:
> > >
> > > Sample;Hole;Feature;Value
> > > 1;5;x;4,2334
> > > 1;5;y;3,3434
> > > 1;5;r;0,1080
> > > 1;10;x;5,2526
> > > 1;10;y;4,3434
> > > 1;10;r;0,1080
> > > ....
> > >
> > > with 98 sample and 10 different holes. These are measured values.
> > >
> > > Now I also have a list of nominel values:
> > >
> > > Hole;Feature;Nominel;LSL;USL
> > > 5;x;4,25,4,20;4,30
> > > 5;y;3,35;3,30;3,40
> > > 5;r;0,10;0,07;0,13
> > > 10;x;5,25;5,20;5,30
> > > ...
> > >
> > > If I want to "normalize" the measured values. The results are to be 
used
> > in
> > > a Sweave file that I plan to reuse for several similar data sets in 
the
> > > furture.
> > >
> > > I have been looking at different approache:
> > > 1. do it in a script out side R
> > > 2. do it the long and hard way by filtering out all the single holes 
and
> > > Feature, the on this list subtract the nominel values, and then 
combine
> > it
> > > back.
> > > 3. ?
> > >
> > > Any help and guidence apriciated
> > > --
> > > Klaus F. Řstergaard, <farremosen(at)gmail dot com>
> > >
> > >       [[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.
> > >
> >
> >
> >
> >
> > 
____________________________________________________________________________________
> >
> > Looking for a deal? Find great prices on flights and hotels with 
Yahoo!
> > FareChase.
> > http://farechase.yahoo.com/
> >
> 
> 
> 
> -- 
> Klaus F. Řstergaard, <farremosen(at)gmail dot com>
> 
>    [[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.



More information about the R-help mailing list