[R] Fwd: data frame row manipulation

Chuck Cleland ccleland at optonline.net
Fri Aug 31 17:02:50 CEST 2007


Calle wrote:
> Hello,
> 
> struggling with the very basic needs... :( any help appreciated.
> 
> #using the package doBY
> #who drinks how much beer per day and therefor cannot calculate rowise
> maxvals
> evaluation=data.frame(date=c(1,2,3,4,5,6,7,8,9),
> name=c("Michael","Steve","Bob",
> "Michael","Steve","Bob","Michael","Steve","Bob"), vol=c(3,5,4,2,4,5,7,6,7))
> evaluation #
> 
> maxval=summaryBy(vol ~ name,data=evaluation,FUN = function(x) { c(ma=max(x))
> } )
> maxval # over all days per person
> 
> #function
> getMaxVal=function(x) { maxval$vol.ma[maxval$name==x] }
> getMaxVal("Steve") # testing the function for one name is ok
> 
> #we want to add a column, that shows the daily drinkingvolume in relation to
> the persons max-vol.
> evaluation[,"relDrink"]= evaluation$vol/getMaxVal(evaluation$name)
> #
> # this brings the error:
> #
> #Warning message:
> # Korrupter Data Frame: Spalten werden abgeschnitten oder mit NAs
> # aufgefüllt in: format.data.frame(x, digits = digits, na.encode = FALSE)
> 
> errortest= evaluation$vol/getMaxVal(evaluation$name)
> errortest
> # this brings:
> # numeric(0)
> 
> 
> #target was the following:
> #show in each line the daily consumed beer per person and in the next column
> 
> #the all time max consumed beer for this person´(or divided by daily vol):
> #
> #  date    name vol relDrink
> #1    1 Michael   3        7
> #2    2   Steve   5        6
> #3    3     Bob   4        7
> #4    4 Michael   2        7
> #5    5   Steve   4        7
> #6    6     Bob   5        7
> #7    7 Michael   7        7
> #8    8   Steve   6        6
> #9    9     Bob   7        7
> 
> # who can help???

  Does this do what you want?

evl <- data.frame(date=c(1,2,3,4,5,6,7,8,9),
                  name=c("M","S","B","M","S","B","M","S","B"),
                  vol=c(3,5,4,2,4,5,7,6,7))

evl <- merge(evl, aggregate(evl$vol,
                            list(name = evl$name),
                            max),
             all=TRUE)

names(evl)[4] <- "MaxDrink"

evl$RelDrink <- with(evl, vol / MaxDrink)

evl
  name date vol MaxDrink  RelDrink
1    B    6   5        7 0.7142857
2    B    3   4        7 0.5714286
3    B    9   7        7 1.0000000
4    M    1   3        7 0.4285714
5    M    7   7        7 1.0000000
6    M    4   2        7 0.2857143
7    S    2   5        6 0.8333333
8    S    5   4        6 0.6666667
9    S    8   6        6 1.0000000

> ------------------------------------------------------------------------
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list