I want to have a dataframe with each time and the sum of the infected individuals.
First I create the vectors that will be in a data frame.
status<-(c("h","h","h","h","i","h","h","h","i","i","h","h","h","i","i","h","h","i","i","i"))
time<-c(0,0,0,0,0,2,2,2,2,2,5,5,5,5,5,8,8,8,8,8)
Then I create the dataframe
test<-data.frame(time,status)
What do I do next? I am open to packages and alternative methods.
Many thanks.
I am not sure which kind of result is the expected output. Below are two options with aggregate
in base R:
> aggregate(.~time,test,list)
time status
1 0 h, h, h, h, i
2 2 h, h, h, i, i
3 5 h, h, h, i, i
4 8 h, h, i, i, i
i
and h
> aggregate(.~time,test,table)
time status.h status.i
1 0 4 1
2 2 3 2
3 5 3 2
4 8 2 3
An option with data.table
library(data.table)
setDT(df)[, .(n = .N), .(time, status)]
Or with count
library(dplyr)
df %>%
count(time, status)
Or in base R
with table
table(df[c('time', 'status')])
Not quite sure what the time variable is representing, but here's an attempt, if I am interpreting your variables correctly.
library(tidyverse)
testB <- test %>%
group_by(status) %>%
summarise(sum = sum(time))
I suppose you want the number of people that are of status h
or i
by time:
library(dplyr)
df %>% group_by(time,status) %>% summarise(n = n())
# A tibble: 8 x 3
# Groups: time [4]
time status n
<dbl> <chr> <int>
1 0 h 4
2 0 i 1
3 2 h 3
4 2 i 2
5 5 h 3
6 5 i 2
7 8 h 2
8 8 i 3
If you are only interested in infected people
df %>% group_by(time,status) %>% summarise(n = n()) %>% filter(status == "i")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.