[R] make new collumns with conditions
Rui Barradas
ru|pb@rr@d@@ @end|ng |rom @@po@pt
Mon Jan 25 16:37:48 CET 2021
Hello,
Try the following.
First aggregate the data, then get the totals, then the percentages.
Finally, put the species in the result.
agg <- aggregate(formula = `duration(s)` ~ `observation nr` + `behavior
type`,
data = d_vigi,
FUN = sum,
subset = `behavior type` == 'Vigilant')
agg$total <- tapply(d_vigi$`duration(s)`, d_vigi$`observation nr`, FUN =
sum)
agg$percent <- round(100 * agg$`duration(s)`/agg$total)
res <- merge(agg, d_vigi[c(1, 3:4)])
res[!duplicated(res), ]
Data in dput format:
d_vigi <-
structure(list(`behavior type` = c("Non-vigilant", "Vigilant",
"Vigilant", "Non-vigilant", "Vigilant", "Vigilant", "Non-vigilant",
"Unkown"), `duration(s)` = c(5L, 2L, 2L, 3L, 7L, 2L, 1L, 2L),
`observation nr` = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), species =
c("red deer",
"red deer", "red deer", "red deer", "red deer", "red deer",
"red deer", "red deer")), class = "data.frame", row.names = c(NA,
-8L))
Hope this helps,
Rui Barradas
Às 13:57 de 25/01/21, krissievdh escreveu:
> Hi,
>
> I have a dataset (d_vigi)with this kind of data:
> behavior type duration(s) observation nr species
> Non-vigilant 5 1 red deer
> Vigilant 2 1 red deer
> Vigilant 2 1 red deer
> Non-vigilant 3 1 red deer
> Vigilant 7 2 red deer
> Vigilant 2 2 red deer
> Non-vigilant 1 2 red deer
> Unkown 2 2 red deer
> Now I have to calculate the percentage of vigilant behavior spent per
> observation.
>
> So eventually I will need to end up with something like this:
> Observation nr Species vigilant(s) total (s) percentage of vigilant (%)
> 1 red deer 4 12 33
> 2 red deer 9 12 75
>
>
> Now I know how to calculate the total amount of seconds per observation.
> But I don't know how I get to the total seconds of vigilant behavior per
> observation (red numbers). If I could get there I will know how to
> calculate the percentage.
>
>
> I calculated the total duration per observation this way:
> for(id in d_vigi$Obs.nr){
>
> d_vigi$t.duration[d_vigi$Obs.nr==id]<-sum(d_vigi$'Duration.(s).x'[d_vigi$Obs.nr==id])
> }
>
> this does work and gives me the total (s) but i don't know how to get to
> the sum of the seconds just for the vigilant per observation number. Is
> there anyone who could help me?
>
> Thanks,
> Krissie
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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