2017-08-09 74 views
1

嗨我需要计算一些实验的累积昆虫日。这是我的数据帧是什么样子如何使用r来计算累计昆虫天数?

Rep trt  date BLB 
1 I 1 7/12/2017 3 
2 I 2 7/12/2017 2 
3 I 3 7/12/2017 4 
4 I 4 7/12/2017 0 
5 II 1 7/12/2017 1 
6 II 2 7/12/2017 2 
7 II 3 7/12/2017 2 
8 II 4 7/12/2017 1 
9 III 1 7/12/2017 3 
10 III 2 7/12/2017 2 
11 III 3 7/12/2017 1 
12 III 4 7/12/2017 1 
13 IV 1 7/12/2017 0 
14 IV 2 7/12/2017 3 
15 IV 3 7/12/2017 3 
16 IV 4 7/12/2017 0 
17 I 1 7/20/2017 12 
18 I 2 7/20/2017 6 
19 I 3 7/20/2017 7 
20 I 4 7/20/2017 18 
21 II 1 7/20/2017 17 
22 II 2 7/20/2017 11 
23 II 3 7/20/2017 25 
24 II 4 7/20/2017 17 
25 III 1 7/20/2017 18 
26 III 2 7/20/2017 6 
27 III 3 7/20/2017 48 
28 III 4 7/20/2017 13 
29 IV 1 7/20/2017 7 
30 IV 2 7/20/2017 22 
31 IV 3 7/20/2017 18 
32 IV 4 7/20/2017 11 
33 I 1 7/27/2017 1 
34 I 2 7/27/2017 3 
35 I 3 7/27/2017 4 
36 I 4 7/27/2017 0 
37 II 1 7/27/2017 1 
38 II 2 7/27/2017 0 
39 II 3 7/27/2017 1 
40 II 4 7/27/2017 0 
41 III 1 7/27/2017 1 
42 III 2 7/27/2017 1 
43 III 3 7/27/2017 0 
44 III 4 7/27/2017 0 
45 IV 1 7/27/2017 1 
46 IV 2 7/27/2017 0 
47 IV 3 7/27/2017 1 
48 IV 4 7/27/2017 2 
49 I 1 8/2/2017 0 
50 I 2 8/2/2017 0 
51 I 3 8/2/2017 1 
52 I 4 8/2/2017 0 
53 II 1 8/2/2017 0 
54 II 2 8/2/2017 0 
55 II 3 8/2/2017 0 
56 II 4 8/2/2017 0 
57 III 1 8/2/2017 1 
58 III 2 8/2/2017 0 
59 III 3 8/2/2017 0 
60 III 4 8/2/2017 0 
61 IV 1 8/2/2017 0 
62 IV 2 8/2/2017 0 
63 IV 3 8/2/2017 0 
64 IV 4 8/2/2017 2 

结构将是:

data.frame': 64 obs. of 4 variables: 
$ Rep : Factor w/ 4 levels "I","II","III",..: 1 1 1 1 2 2 2 2 3 3 ... 
$ trt : Factor w/ 4 levels "1","2","3","4": 1 2 3 4 1 2 3 4 1 2 ... 
$ date: Factor w/ 4 levels "7/12/2017","7/20/2017",..: 1 1 1 1 1 1 1 1 1 1 ... 
$ BLB : int 3 2 4 0 1 2 2 1 3 2 ... 

要做到这一点,我需要计算昆虫的平均值为不同的处理日期的每个组合。例如,我必须计算每次治疗的日期7/12和7/20之间的每一次。然后我需要计算日期7/20和7/27之间的平均值等等。有没有人知道如何使用r软件来做到这一点?我真的很感谢帮助!

+0

那么你是否将7/12和7/20视为一组?另有7/20和7/27? – useR

+0

是的,我将两个日期的每个组合作为一个组对待! – Obiratanea

+1

下次你可以使用'dput(dataframe)'? – CPak

回答

1

首先创建数据(如果你提供的dput(数据将是很好)...):

set.seed(123) 
df = data.frame(Rep = rep(c("I","II","III","IV"), each = 4, times = 4), 
       trt = as.factor(rep(1:4, times = 16)), 
       date = as.Date(rep(c("7/12/2017", "7/20/2017", "7/27/2017", "8/2/2017"), each = 16), 
           format = "%m/%d/%Y"), 
       BLB = sample(0:50, 64, replace = TRUE)) 

> str(df) 
'data.frame': 64 obs. of 4 variables: 
$ Rep : Factor w/ 4 levels "I","II","III",..: 1 1 1 1 2 2 2 2 3 3 ... 
$ trt : Factor w/ 4 levels "1","2","3","4": 1 2 3 4 1 2 3 4 1 2 ... 
$ date: Date, format: "2017-07-12" "2017-07-12" "2017-07-12" ... 
$ BLB : int 14 40 20 45 47 2 26 45 28 23 ... 

简单的子集和汇总:

# Create subset for each date group 
date_group1 = subset(df, df$date %in% c(as.Date("2017-07-12"), 
             as.Date("2017-07-20"))) 
date_group2 = subset(df, df$date %in% c(as.Date("2017-07-20"), 
             as.Date("2017-07-27"))) 
date_group3 = subset(df, df$date %in% c(as.Date("2017-07-27"), 
             as.Date("2017-08-02"))) 

# Aggregate by treatment in each date_group 
aggregate(BLB ~ trt, data = date_group1, mean) 
aggregate(BLB ~ trt, data = date_group2, mean) 
aggregate(BLB ~ trt, data = date_group3, mean) 

# > aggregate(BLB ~ trt, data = date_group1, mean) 
# trt BLB 
# 1 1 28.375 
# 2 2 21.750 
# 3 3 27.875 
# 4 4 41.500 
# > aggregate(BLB ~ trt, data = date_group2, mean) 
# trt BLB 
# 1 1 23.875 
# 2 2 19.875 
# 3 3 21.625 
# 4 4 31.250 
# > aggregate(BLB ~ trt, data = date_group3, mean) 
# trt BLB 
# 1 1 22.375 
# 2 2 21.250 
# 3 3 17.875 
# 4 4 17.500 
+0

非常感谢你!它为我工作! – Obiratanea

+0

@Obiratanea如果你认为这回答你的问题。请接受它,让其他人可以看到。 – useR

+0

useR我怎么接受它?谢谢 – Obiratanea

0

你已经错过了一些日期组合组@用户

有 (2017年7月12日,2017年7月27日), (2017年7月12日,2017年8月2日), (2017年7月20日,2017年8月2日)也。

+0

OP提到“between日期7/12和7/20“和”日期7/20和7/27之间“。所以我认为他只需要连续配对,因为从2017-07-12到2017-07-27将包括2017-07-20。 – useR