[R] data frame row manipulation

Gabor Grothendieck ggrothendieck at gmail.com
Fri Aug 31 17:06:29 CEST 2007


Try this:

evaluation$maxVol <- ave(evaluation$vol, evaluation$name, FUN = max)

or using SQL via sqldf like this:

library(sqldf)
sqldf("select * from evaluation join
  (select name, max(vol) from evaluation group by name) using (name)")


On 8/31/07, Calle <calle.scharffenorth at gmail.com> 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???
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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.
>
>



More information about the R-help mailing list