Search code examples
rmeantapply

Problems calculating means using tapply() in R


I am trying to calculate means on a data.frame using the tapply() function.

Here is my 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

Here is the same dataset outputted using 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")

What I want to do is to calculate the mean for the 'net mass (g)' variable according to two variables with 2 factor-levels each : 1)'locale', with levels "ELC" and "Calico Jack"; 2) 'sex', with levels "M" and "F". In other words, I would expect to end up with 4 mean values. Here is the tapply() function that I am using (and that I have successfully used on other database but this one).

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

Unfortunately I do not get the mean values. Instead this nasty error message comes up, and I am really frustrated because I cannot figure out what I am doing wrong with this data set.

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

Any help would be greatly appreciated.

Cheers


Solution

  • I slightly modified your sample dataframe, but I think with my last line, the aggregate() function, will get you where you want:

     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
    

    With tapply:

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

    OR

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

    Both give:

                     F        M
    ELC           763.3333 1103.333
    other locale 1600.0000 1900.000