[R] beginner question: subset first entry (row) per week - found the answer myself :-)

Dagmar Ramgad82 at gmx.net
Fri Feb 3 09:05:58 CET 2017


Dear all,

Now, all the sudden I found the answer myself :-)

I did it by:

ddply(exdatframe,.(week),function(x) head(x,1))

I'll simply do it for Ernie and Cookiemonster seperate - that is not a 
big problem.

Thanks anyway for your engagement in this group!

Dagmar


Am 02.02.2017 um 22:04 schrieb Dagmar:
> Dear knowing people,
>
> I have a data frame like this.
>
> exdatframe <- data.frame(Name=c("Ernie","Ernie","Ernie", 
> "CookieMonster","CookieMonster","CookieMonster"),
> recordedTime=as.POSIXct(strptime(as.character("01.01.2017","02.01.2011","03.01.2011", 
>
> "01.01.2011","02.01.2011","03.01.2011"),"%d.%m.%Y")),
>                          week =c(1,2,2,
>                                 1,2,2),
>                          eatencookies=c(1,0.5,0.001,
>                                        50,51,200))
> exdatframe
>
> #Now I want a new dataframe with only the first row per week (i.e. I 
> want to know how many cookies were eaten at the first recorded day of 
> each week). Something like that:
>
> exdatframe2 <- data.frame(Name=c("Ernie","Ernie", 
> "CookieMonster","CookieMonster"),
>                          recordedTime=c("01.01.2017","02.01.2011",
> "01.01.2011","02.01.2011"),
>                          week =c(1,2,
>                                  1,2),
>                          eatencookies=c(1,0.5,
>                                         50,51))
> exdatframe2
>
> # How do I do that? I thought it must be something with tapply or 
> subset - but I just don't get it....
>
> # would be great if someone helps.
>
> # Dagmar
>
>



More information about the R-help mailing list