计算问题意味着在R

时间:2018-05-31 11:30:14

标签: r mean tapply

我正在尝试使用tapply()函数计算data.frame上的均值。

这是我的数据框。

   record            island      locale capture_type age_at_capt date_yyyy_mm_dd sex net mass (g) svl (mm)
6    1939 Big Ambergris Cay         ELC        recap       Adult      2018-03-24   F          740      254
7    1940 Big Ambergris Cay         ELC        recap       Adult      2018-03-24   F          815      270
8    1941 Big Ambergris Cay         ELC        recap       Adult      2018-03-24   F          735      262
9    1942 Big Ambergris Cay         ELC        recap       Adult      2018-03-24   M         1440      330
10   1943 Big Ambergris Cay         ELC       newcap       Adult      2018-03-25   M         1060      284
11   1944 Big Ambergris Cay         ELC       newcap       Adult      2018-03-25   M          810      275
12   1945 Big Ambergris Cay         ELC        recap       Adult      2018-03-25   M         1375      310
13   1946 Big Ambergris Cay         ELC        recap       Adult      2018-03-25   M         1395      325
14   1947 Big Ambergris Cay         ELC        recap       Adult      2018-03-25   F          622      257
15   1948 Big Ambergris Cay         ELC        recap       Adult      2018-03-25   M         1120      294
16   1949 Big Ambergris Cay         ELC       newcap       Adult      2018-03-25   F          690      247
18   1951 Big Ambergris Cay         ELC        recap       Adult      2018-03-26   F          935      274
19   1952 Big Ambergris Cay Calico Jack        recap       Adult      2018-03-27   M         2505      370
20   1953 Big Ambergris Cay Calico Jack       newcap       Adult      2018-03-27   F         1110      279
21   1954 Big Ambergris Cay Calico Jack       newcap       Adult      2018-03-27   F         1590      313
22   1955 Big Ambergris Cay Calico Jack        recap       Adult      2018-03-27   M         1575      319
23   1956 Big Ambergris Cay Calico Jack       newcap       Adult      2018-03-27   M         1110      284
24   1957 Big Ambergris Cay Calico Jack        recap       Adult      2018-03-27   M         2380      357
25   1958 Big Ambergris Cay Calico Jack        recap       Adult      2018-03-27   M         2080      364
26   1959 Big Ambergris Cay Calico Jack       newcap       Adult      2018-03-27   F         1010      286
27   1960 Big Ambergris Cay Calico Jack       newcap       Adult      2018-03-27   M          830      259
28   1961 Big Ambergris Cay Calico Jack       newcap       Adult      2018-03-27   M         1850      348
29   1962 Big Ambergris Cay Calico Jack        recap       Adult      2018-03-27   F         1250      296
30   1963 Big Ambergris Cay Calico Jack        recap       Adult      2018-03-27   F         1075      282
31   1964 Big Ambergris Cay Calico Jack        recap       Adult      2018-03-28   M         2240      362
32   1965 Big Ambergris Cay Calico Jack        recap       Adult      2018-03-28   M         1775      338
33   1966 Big Ambergris Cay Calico Jack        recap       Adult      2018-03-28   F         1420      300
34   1967 Big Ambergris Cay Calico Jack        recap       Adult      2018-03-28   F         1010      263
35   1968 Big Ambergris Cay Calico Jack        recap       Adult      2018-03-28   M         2090      372
36   1969 Big Ambergris Cay Calico Jack       newcap       Adult      2018-03-28   F         1440      304
37   1970 Big Ambergris Cay Calico Jack        recap       Adult      2018-03-28   M          755      254
38   1971 Big Ambergris Cay Calico Jack       newcap       Adult      2018-03-28   F         1360      315
40   1973 Big Ambergris Cay Calico Jack        recap       Adult      2018-03-28   M         2700      371
41   1974 Big Ambergris Cay Calico Jack        recap       Adult      2018-03-28   M         1820      331
42   1975 Big Ambergris Cay         ELC        recap       Adult      2018-03-29   M         1450      326
43   1976 Big Ambergris Cay         ELC        recap       Adult      2018-03-29   F          790      262
44   1977 Big Ambergris Cay         ELC        recap       Adult      2018-03-29   F          605      246

这是使用dput()

输出的相同数据集
structure(list(record = c(1939, 1940, 1941, 1942, 1943, 1944, 
1945, 1946, 1947, 1948, 1949, 1951, 1952, 1953, 1954, 1955, 1956, 
1957, 1958, 1959, 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 
1968, 1969, 1970, 1971, 1973, 1974, 1975, 1976, 1977), island = c("Big    Ambergris Cay", 
"Big Ambergris Cay", "Big Ambergris Cay", "Big Ambergris Cay", 
"Big Ambergris Cay", "Big Ambergris Cay", "Big Ambergris Cay", 
"Big Ambergris Cay", "Big Ambergris Cay", "Big Ambergris Cay", 
"Big Ambergris Cay", "Big Ambergris Cay", "Big Ambergris Cay", 
"Big Ambergris Cay", "Big Ambergris Cay", "Big Ambergris Cay", 
"Big Ambergris Cay", "Big Ambergris Cay", "Big Ambergris Cay", 
"Big Ambergris Cay", "Big Ambergris Cay", "Big Ambergris Cay", 
"Big Ambergris Cay", "Big Ambergris Cay", "Big Ambergris Cay", 
"Big Ambergris Cay", "Big Ambergris Cay", "Big Ambergris Cay", 
"Big Ambergris Cay", "Big Ambergris Cay", "Big Ambergris Cay", 
"Big Ambergris Cay", "Big Ambergris Cay", "Big Ambergris Cay", 
"Big Ambergris Cay", "Big Ambergris Cay", "Big Ambergris Cay"
), locale = c("ELC", "ELC", "ELC", "ELC", "ELC", "ELC", "ELC", 
"ELC", "ELC", "ELC", "ELC", "ELC", "Calico Jack", "Calico Jack", 
"Calico Jack", "Calico Jack", "Calico Jack", "Calico Jack", "Calico Jack", 
"Calico Jack", "Calico Jack", "Calico Jack", "Calico Jack", "Calico Jack", 
"Calico Jack", "Calico Jack", "Calico Jack", "Calico Jack", "Calico Jack", 
"Calico Jack", "Calico Jack", "Calico Jack", "Calico Jack", "Calico Jack", 
"ELC", "ELC", "ELC"), capture_type = c("recap", "recap", "recap", 
"recap", "newcap", "newcap", "recap", "recap", "recap", "recap", 
"newcap", "recap", "recap", "newcap", "newcap", "recap", "newcap", 
"recap", "recap", "newcap", "newcap", "newcap", "recap", "recap", 
"recap", "recap", "recap", "recap", "recap", "newcap", "recap", 
"newcap", "recap", "recap", "recap", "recap", "recap"), age_at_capt =  c("Adult", 
"Adult", "Adult", "Adult", "Adult", "Adult", "Adult", "Adult", 
"Adult", "Adult", "Adult", "Adult", "Adult", "Adult", "Adult", 
"Adult", "Adult", "Adult", "Adult", "Adult", "Adult", "Adult", 
"Adult", "Adult", "Adult", "Adult", "Adult", "Adult", "Adult", 
"Adult", "Adult", "Adult", "Adult", "Adult", "Adult", "Adult", 
"Adult"), date_yyyy_mm_dd = c("2018-03-24", "2018-03-24", "2018-03-24", 
"2018-03-24", "2018-03-25", "2018-03-25", "2018-03-25", "2018-03-25", 
"2018-03-25", "2018-03-25", "2018-03-25", "2018-03-26", "2018-03-27", 
"2018-03-27", "2018-03-27", "2018-03-27", "2018-03-27", "2018-03-27", 
"2018-03-27", "2018-03-27", "2018-03-27", "2018-03-27", "2018-03-27", 
"2018-03-27", "2018-03-28", "2018-03-28", "2018-03-28", "2018-03-28", 
"2018-03-28", "2018-03-28", "2018-03-28", "2018-03-28", "2018-03-28", 
"2018-03-28", "2018-03-29", "2018-03-29", "2018-03-29"), sex = c("F", 
"F", "F", "M", "M", "M", "M", "M", "F", "M", "F", "F", "M", "F", 
"F", "M", "M", "M", "M", "F", "M", "M", "F", "F", "M", "M", "F", 
"F", "M", "F", "M", "F", "M", "M", "M", "F", "F"), `net mass (g)` = c(740, 
815, 735, 1440, 1060, 810, 1375, 1395, 622, 1120, 690, 935, 2505, 
1110, 1590, 1575, 1110, 2380, 2080, 1010, 830, 1850, 1250, 1075, 
2240, 1775, 1420, 1010, 2090, 1440, 755, 1360, 2700, 1820, 1450, 
790, 605), `svl (mm)` = c(254, 270, 262, 330, 284, 275, 310, 
325, 257, 294, 247, 274, 370, 279, 313, 319, 284, 357, 364, 286, 
259, 348, 296, 282, 362, 338, 300, 263, 372, 304, 254, 315, 371, 
331, 326, 262, 246)), row.names = c(6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 16L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 
26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 
40L, 41L, 42L, 43L, 44L), class = "data.frame")

我想要做的是计算净质量的平均值(g)'根据两个变量变量,每个变量具有2个因子级别:1)' locale',具有级别" ELC"和" Calico Jack&#34 ;; 2)'性别',等级" M"和" F"。换句话说,我希望最终得到4个均值。这是我正在使用的tapply()函数(我已经成功地在其他数据库上使用了这个函数)。

MeanMass.2018 <- tapply(Records.2018.BA.ad$`net mass (g)`, 
                    INDEX = Records.2018.BA.ad$locale : Records.2018.BA.ad$sex, 
                    FUN   = mean)

不幸的是我没有得到平均值。相反,这个讨厌的错误消息出现了,我真的很沮丧,因为我无法弄清楚我对这个数据集做错了什么。

Error in Records.2018.BA.ad$locale:Records.2018.BA.ad$sex : 
 NA/NaN argument
In addition: Warning messages:
1: In Records.2018.BA.ad$locale:Records.2018.BA.ad$sex :
 numerical expression has 37 elements: only the first used
2: In Records.2018.BA.ad$locale:Records.2018.BA.ad$sex :
 numerical expression has 37 elements: only the first used
3: In tapply(Records.2018.BA.ad$`net mass (g)`, INDEX =     Records.2018.BA.ad$locale:Records.2018.BA.ad$sex,  :
 NAs introduced by coercion
4: In tapply(Records.2018.BA.ad$`net mass (g)`, INDEX =    Records.2018.BA.ad$locale:Records.2018.BA.ad$sex,  :
NAs introduced by coercion

非常感谢任何帮助。

干杯

1 个答案:

答案 0 :(得分:1)

我稍微修改了您的示例数据框,但我认为使用我的最后一行,aggregate()函数可以获得您想要的位置:

 df <- read.table(text = "
record island locale capture_type age_at_capt date_yyyy_mm_dd sex netmass svl 
1939 BigAmbergrisCay    ELC        recap       Adult      2018-03-24   F          740      254
1940 BigAmbergrisCay    ELC        recap       Adult      2018-03-24   F          815      270
1941 BigAmbergrisCay    ELC        recap       Adult      2018-03-24   F          735      262
1942 BigAmbergrisCay    ELC        recap       Adult      2018-03-24   M         1440      330
1943 BigAmbergrisCay    ELC       newcap       Adult      2018-03-25   M         1060      284
1944 BigAmbergrisCay    ELC       newcap       Adult      2018-03-25   M          810      275
", header = T)

df <- rbind(df, df)

df$locale <- as.character(df$locale)
df$locale[7:12] <- "other locale"
df$netmass[7:12] <- seq(1500,2000,100)


aggregate(netmass ~ locale + sex, df, mean)


       locale sex   netmass
1         ELC   F  763.3333
2 otherlocale   F 1600.0000
3         ELC   M 1103.3333
4 otherlocale   M 1900.0000

用tapply:

tapply(df$netmass, list(df$locale, df$sex), mean)

OR

with(df, tapply(netmass, list(locale, sex), mean))

两者都给:

                 F        M
ELC           763.3333 1103.333
other locale 1600.0000 1900.000