[R] adding rows as arithmatic calculation on original rows

jim holtman jholtman at gmail.com
Fri Dec 5 23:05:00 CET 2008


This should get you close:

> x <- read.table(textConnection("myID myType myNum1 myNum2 myNum3
+ a       Single       10          11           12
+ b       Single       15          25           35
+ c       Double      22          33           44
+ d       Double        4           6             8"), header=TRUE)
> closeAllConnections()
> y <- lapply(split(x, x$myType), function(.type){
+     .means <- colMeans(.type[,3:5])
+     # create the new line for the data frame
+     .df <- data.frame(myID='', myType=.type$myType[1], myNum1=.means[1],
+         myNum2=.means[2], myNum3=.means[3])
+     rbind(.type, .df) # append the line to the original dataframe
+ })
> do.call(rbind, y)  # you can add the names your want
              myID myType myNum1 myNum2 myNum3
Double.3         c Double   22.0   33.0   44.0
Double.4         d Double    4.0    6.0    8.0
Double.myNum1      Double   13.0   19.5   26.0
Single.1         a Single   10.0   11.0   12.0
Single.2         b Single   15.0   25.0   35.0
Single.myNum1      Single   12.5   18.0   23.5


On Fri, Dec 5, 2008 at 3:21 PM, Ferry <fmi.mlist at gmail.com> wrote:
> Dear R users,
>
> Suppose I have the following data.frame:
>
> myID myType myNum1 myNum2 myNum3
> a       Single       10          11           12
> b       Single       15          25           35
> c       Double      22          33           44
> d       Double        4           6             8
>
> and I want to have new records:
>
> myID myType myNum1 myNum2 myNum3
> e      Single       12.5       18           23.5
> f       Double      13          19.5         28
>
> where record e got its myNum1-3 as the average from record a and b, and
> record f got its myNum1-3 as the average from record c and d.
>
> and the final data.frame should be like the following:
>
> myID myType myNum1 myNum2 myNum3
> a       Single       10          11           12
> b       Single       15          25           35
> e       Single       12.5       18           43.5
> c       Double      22          33           44
> d       Double        4           6             8
> f        Double      13          19.5         28
>
> Any idea is appreciated. Thanks beforehand.
>
> Ferry
>
>        [[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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list