Skip to content
Prev 320598 / 398506 Next

Dataframe manipulation

Hi Adam,

I hope this is what you wanted:
dat1<- read.csv("example.csv",sep="\t",stringsAsFactors=FALSE)
?str(dat1)
#'data.frame':??? 102 obs. of? 5 variables:
# $ species? : chr? "B. barbastrellus" "E. nilssonii" "H. savii" "M. alcathoe" ...
# $ period?? : chr? "dusk" "dusk" "dusk" "dusk" ...
# $ treatment: chr? "control" "control" "control" "control" ...
# $ no.files : int? 16 1 9 13 1 49 6 3 4 0 ...
# $ expected : logi? NA NA NA NA NA NA ...
?dat2<-within(dat1,{expected<-ave(no.files,species,treatment,FUN=mean)})

head(dat2)
#?????????? species period treatment no.files? expected
#1 B. barbastrellus?? dusk?? control?????? 16 14.333333
#2???? E. nilssonii?? dusk?? control??????? 1? 1.000000
#3???????? H. savii?? dusk?? control??????? 9? 4.666667
#4????? M. alcathoe?? dusk?? control?????? 13 13.333333
#5?? M. bechsteinii?? dusk?? control??????? 1? 5.666667
#6????? M. brandtii?? dusk?? control?????? 49 63.000000
A.K.