[R] beginner question: subset first entry (row) per week

Dagmar Ramgad82 at gmx.net
Thu Feb 2 22:04:28 CET 2017


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