[R] create a new dataframe with intervals and computing a weighted average for each of its rows
Jeff Newmiller
jdnewmil at dcn.davis.CA.us
Sun Nov 24 20:54:38 CET 2013
Well, in his defense, he did provide quite a bit of R code, and did use a data.frame function to build a sample input data frame, so there was some effort made to communicate.
Unfortunately, after inserting newlines in the code that were demolished by the HTML, the code still does not run because it references a VALUE vector that is missing. By the name this seems like it might be important, but you don't reference it later so it is hard to guess what he wants to do.
Luis:
Your code is quite complicated after that, but I think you are doing way too much work on dividing up your data into groups since the ave function, aggregate function or the plyr library can simplify this very much. Since I don't see what you want to DO with each group, it is hard to show you a simpler way to do it. Perhaps you should just work through the examples for some of those functions.
Note that the only reliable way to give us sample data is using the dput function (or the data.frame function). Formatted tables pasted from spreadsheets just don't get through the email list clearly. Nor do most attachments.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
Bert Gunter <gunter.berton at gene.com> wrote:
>This post is complete garbage, and a great example of why not
>bothering to read or follow the posting guide will cause a post to be
>ignored.
>
>1. It was not posted in plain text as the posting guide asks.
>
>2. dput() was not used to pass example data
>
>3. It appears the OP has not done due diligence by going through the
>Introduction to R or other online tutorials to learn how R works,
>although the post was so garbled that I may be wrong about that. My
>apology, if so.
>
>Cheers,
>Bert
>
>
>
>On Sun, Nov 24, 2013 at 1:08 AM, Luis Miguel Cerchiaro Barros
><luis_cerchiaro at hotmail.com> wrote:
>>
>>
>>
>>
>> I need you help with this problem, I have a data-frame like this:
>> BHID=c(43,43,43,43,44,44,44,44,44)
>FROM=c(50.9,46.7,44.2,43.1,52.3,51.9,49.3,46.2,42.38)
>TO=c(46.7,44.2,43.1,40.9,51.9,49.3,46.2,42.38,36.3)
>AR=c(45,46,0.0,38.45,50.05,22.9,0,25,9)
>DF<-data.frame(BHID,FROM,TO,VALUE) #add the length
>DF$LENGTH=DF$FROM-DF$TO
>> where:
>> + BHID: is the borehole identification+ FROM: is the start for every
>interval+ TO: is the end for every interval+ AR: is the value of our
>variable+ LENGTH: is the distance between FROM and TO
>> what I want, is create a data frame which is "normalized", it means
>that every interval has the same length and the column **AR** is
>calculated as a Weighted arithmetic mean from the old **AR** and
>**LENGTH** as its weight.
>> For more clarity I going to show you how should look the desire data
>frame.
>> BHID FROM TO AR LENGTH 43
>50.9 47.9 45.0 3.0 43 47.9 44.9
>45.6 3.0 43 44.9 41.9 26.113 3.0 43
> 41.9 40.9 38.45 1.0 44....
>> where:
>> 1. AR is the Weighted arithmetic mean
>> I have to make a clarification about the result:
>> here I attached an example of my excel table with calculations:
>> ROW_ID BHID NEW_FROM NEW_TO NEW_AR OLD_FROM OLD_TO WEIGHTS
>OLD_AR 1 43 50.9 47.9 45
>50.9 46.7 3.0 45 2 43 47.9
>44.9 45.6 50.9 46.7 1.2
>45 2 43 47.9 44.9
>46.7 44.2 1.8 46 3 43 44.9
>41.9 26.113 46.7 44.2 0.7 46 3
>43 44.9 41.9 44.2 43.1
>1.1 0 3 43 44.9 41.9
>43.1 40.9 1.2 38.45 4 43 41.9
> 40.9 38.45 43.1 40.9 1.0 38.45
>>
>> you see guys, the NEW_AR is the weighted mean of the OLD_AR and its
>weights are in the column WEIGHTS.
>> If you see the column LENGTH in the original data frame you can see,
>that the values are different, with the "normalization" we try to make
>that LENGTH uniform, in this case we choose the value 3.0 of course the
>last value of each borehole data could had a different LENGTH in this
>case 1.0
>> What I have done to achieve the result
>> OK guys in first place I have to say, I am not a professional and I
>am still learning how to use R,
>> my approximation is not elegant, I am trying to take the start and
>end of each borehole and use the function skeleton what I wrote, to
>create an uniform skeleton for the whole dataframe.
>> skeleton<-function(DF,LEN){ # define function to create a new
>skeleton divide.int<-function(FROM,TO,div){
>n=as.integer((FROM-TO)/div)+1 from=seq(FROM,(FROM-(n-1)*div),-div)
>to=seq(FROM-(n-(n-1))*div,FROM-(n-1)*div,-div) to[n]=TO
>range<-data.frame(BHID=borehole_names[i,1],FROM=from,TO=to) # create a
>data.frame class object range<-range[!(range$FROM==range$TO),] #
>erase the last value } # subset the data set for every borehole
>borehole_names<-unique(DF["BHID"]) # collars id with cores
>borehole_number<-nrow(borehole_names) # collar number #define an
>empty data.frame
>borehole_Out<-data.frame(BHID=integer(),FROM=numeric(),TO=numeric())
># initialize the counter i=1 # from this point starts the
>loop--------------- while(i<=borehole_number){ DFi <- subset(DF,
>BHID %in% borehole_names[i,1]) # Individual data frame for each
>boreholes # take the beginning and end of every BOREHOLE
>startBH<-head(DFi$FROM,1) endBH<!
>
>
> -t!
>> ail(DFi$TO,1) # create the normalized intervals
>borehole_i<-divide.int(FROM=startBH,TO=endBH,div=LEN)
>borehole_Out<-rbind(borehole_Out,borehole_i) i=i+1 }
>borehole_Out } # TEST------------------------------------------
> TEST<-skeleton(DF=DF,LEN=3.0) TEST$LENGTH=TEST$FROM-TEST$TO
>> later I am trying to use the packages PLYR or DATA.TABLE to calculate
>the weighted means in AR but as I said I just started to use R and
>don't understand yet how this packages work
>> again thanks in advanced and sorry for my bumpy english
>>
>>
>>
>>
>> [[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