0

I've discovered an issue when calculating sum of rows and counts (">0") in my data frame. I'm trying to sum rows to have abundance and richness of a community data "spe" (51 columns(taxa) and 82 rows(sites)). I realize this when double checking the sums and counts in R and Excel. Why is this happening? This is the code I use in R:

For Sum:
apply(spe[,-1],1,sum)
For Counts:
apply(spe[,-1]>0,1,sum)

And here are the results I have: pic of the results I had with R and Excel

I've noticed the first rows goes well with the sums and counts, but then the results in R are different by subtracting numbers in sums and just 1 less in the counts issue, and after some rows, there are some good with the sums and counts but not all of them comparing to Excel. Why is that happening? Can someone help me please? Thanks in advance.

spe<-data.frame(read.table("~/Dropbox/Docs Redro/Paper Tesis MBC/MBC_SPP_ALL.csv", sep=",", header=TRUE, row.names=1))
head(spe)
#>   Duge Allua1 Ande Anom Atop1 Atop2 Bleph1 Bleph2 Bleph3 Caillo2 Caillo3
#> 2    0      0    0    0     1     0      0      0      0       0       0
#> 3    0      0    0    0     0     0      0      0      0       0       0
#> 4    0      0    0    0     0     0      0      0      0       0       0
#> 5    0      0    0    0     0     0      0      0      0       0       0
#> 8    0      0    0    0     0     0      0      0      0       0       0
#> 9    0      0    0    0     0     0      0      0      0       0       0
#>   Caillo1 Ceratoindet Cheli2 chiroindet Chiroinae Claudio Contu Corix2 Crambi
#> 2       2           0      1          0         0       0     0      0      0
#> 3       0           0      0          0         0       0     0      0      0
#> 4       3           0      2          0         0       0     0      0      0
#> 5       0           0      0          0         0       0     0      0      0
#> 8       6           0      0          0         0       0     0      0      0
#> 9       0           0      0          0         0       0     0      0      0
#>   Curcu1 Curcu2 Diame1 Diame2 Dimec Dytis1 Ephyd2 Geran Helicho Hemer1 Hyale
#> 2      0      0      1      0     0      0      0     0       0      2     0
#> 3      0      0      0      0     0      0      0     0       0      0     0
#> 4      0      0      2      1     0      0      0     0       0      1     0
#> 5      0      0      0      0     0      0      0     0       0      0     0
#> 8      0      0      0      0     0      1      0     0       0      0     0
#> 9      0      0      1      0     0      0      0     0       0      0     0
#>   HYDRA Hydrosc Leech1 Leech2 Leech3 Leech4 Leuco Limoniindet Lumbri Lymnae
#> 2     0       0      0      0      0      0     0           0      0      0
#> 3     0       0      0      0      0      0     0           0      0      0
#> 4     0       0      0      0      2      0     0           0      0      0
#> 5     0       0      0      0      0      0     0           0      0      0
#> 8     0       0      0      0      0      0     0           0      0      0
#> 9     0       0      0      0      0      0     0           0      0      0
#>   Maya Molo1 Molo2 Morto Musci5 Musci6 Musci7 Musci4 Musci1 Musci2 Musci3 Naid1
#> 2    0     1     0     0      0      0      0      1     11      0      0    39
#> 3    0     0     0     0      0      0      0      0      0      0      0     0
#> 4    0     0     0     0      0      0      0      0      8      0      0    32
#> 5    0     0     0     0      0      0      0      1      0      0      0     0
#> 8    0     0     1     0      0      0      0      0      2      0      0     0
#> 9    0     0     0     0      0      0      0      0      0      0      0     0
#>   Naid2 Naid3 Nectop Nema Neoel1 Neoel2 Neot Nepti Nona1 Ochro Ortho Ostra
#> 2     0     0      0    2      0      0    0     0     0     0    72     0
#> 3     0     0      0    0      0      0    0     0     0     0     0     0
#> 4     0     0      0    4      0      1    0     0     0     0    85     0
#> 5     0     0      0    0      0      0    0     0     0     0     7     0
#> 8     0     0      0    2      0      0    0     0     0     0    43     0
#> 9     0     0      0    0      0      0    0     0     0     0     2     0
#>   Podo1 Podo2 Podo3 Podo4 Priono1 Priono2 Priono3 Schoe Simu1 Sphae Staphy1
#> 2    24     0     4     1       0       0       0     0   124     0       1
#> 3    16     0    28     0       0       0       0     0     0     0       0
#> 4   177     0     2     2       0       0       0     0    18     0       0
#> 5    13     0    71     1       0       0       0     0     0     0       0
#> 8    46     0     3     0       0       0       0     0     3     0       0
#> 9    49     0    20    14       0       0       0     0     0     0       1
#>   Staphy2 Stilo1 Sync1 Tanyp Tipu Trichoco
#> 2       0      0     0     0    0        0
#> 3       0      0     0     0    0        0
#> 4       0      0     0     0    0        0
#> 5       0      0     0     0    0        0
#> 8       0      0     0     0    0        0
#> 9       0      0     0     0    0        0

# Spp Richness
apply(spe[,-1]>0,1,sum)
#>  2  3  4  5  8  9  1  7 37 38 47 24 46 43 45 50 40 42 14 49 51 29 30 31 33  6 
#> 16  2 15  5  9  6 17 17 10 15 19 33 27 34 31 27 20 25 32 29 30 26 35 34 25 20 
#> 41 26 27 34 35 10 15 16 17 18 19 20 21 22 25 28 32 36 48 13 12 11 23 39 44 
#> 24 22 24 29 26 18 25 26 24 33 26 24 16 27 21 36 14 31 25 33 15 27 30 17 21

# Abundance
apply(spe[,-1],1,sum)
#>    2    3    4    5    8    9    1    7   37   38   47   24   46   43   45   50 
#>  287   44  340   93  107   87  214  268  112   58  788 1554 1225 2683  990 2757 
#>   40   42   14   49   51   29   30   31   33    6   41   26   27   34   35   10 
#>  213  348 1508 1479 1459 1026 2961 2807 2586  760  629 2074 1254 2104 9976  531 
#>   15   16   17   18   19   20   21   22   25   28   32   36   48   13   12   11 
#> 1645 4161 1371 4132 1407 3807 4047 1619 4314 3057  232 3348 1457 1633 2240 2084 
#>   23   39   44 
#> 1734  553 2774

dput(head(spe))
#> structure(list(Duge = c(0L, 0L, 0L, 0L, 0L, 0L), Allua1 = c(0L, 
#> 0L, 0L, 0L, 0L, 0L), Ande = c(0L, 0L, 0L, 0L, 0L, 0L), Anom = c(0L, 
#> 0L, 0L, 0L, 0L, 0L), Atop1 = c(1L, 0L, 0L, 0L, 0L, 0L), Atop2 = c(0L, 
#> 0L, 0L, 0L, 0L, 0L), Bleph1 = c(0L, 0L, 0L, 0L, 0L, 0L), Bleph2 = c(0L, 
#> 0L, 0L, 0L, 0L, 0L), Bleph3 = c(0L, 0L, 0L, 0L, 0L, 0L), Caillo2 = c(0L, 
#> 0L, 0L, 0L, 0L, 0L), Caillo3 = c(0L, 0L, 0L, 0L, 0L, 0L), Caillo1 = c(2L, 
#> 0L, 3L, 0L, 6L, 0L), Ceratoindet = c(0L, 0L, 0L, 0L, 0L, 0L), 
#>     Cheli2 = c(1L, 0L, 2L, 0L, 0L, 0L), chiroindet = c(0L, 0L, 
#>     0L, 0L, 0L, 0L), Chiroinae = c(0L, 0L, 0L, 0L, 0L, 0L), Claudio = c(0L, 
#>     0L, 0L, 0L, 0L, 0L), Contu = c(0L, 0L, 0L, 0L, 0L, 0L), Corix2 = c(0L, 
#>     0L, 0L, 0L, 0L, 0L), Crambi = c(0L, 0L, 0L, 0L, 0L, 0L), 
#>     Curcu1 = c(0L, 0L, 0L, 0L, 0L, 0L), Curcu2 = c(0L, 0L, 0L, 
#>     0L, 0L, 0L), Diame1 = c(1L, 0L, 2L, 0L, 0L, 1L), Diame2 = c(0L, 
#>     0L, 1L, 0L, 0L, 0L), Dimec = c(0L, 0L, 0L, 0L, 0L, 0L), Dytis1 = c(0L, 
#>     0L, 0L, 0L, 1L, 0L), Ephyd2 = c(0L, 0L, 0L, 0L, 0L, 0L), 
#>     Geran = c(0L, 0L, 0L, 0L, 0L, 0L), Helicho = c(0L, 0L, 0L, 
#>     0L, 0L, 0L), Hemer1 = c(2L, 0L, 1L, 0L, 0L, 0L), Hyale = c(0L, 
#>     0L, 0L, 0L, 0L, 0L), HYDRA = c(0L, 0L, 0L, 0L, 0L, 0L), Hydrosc = c(0L, 
#>     0L, 0L, 0L, 0L, 0L), Leech1 = c(0L, 0L, 0L, 0L, 0L, 0L), 
#>     Leech2 = c(0L, 0L, 0L, 0L, 0L, 0L), Leech3 = c(0L, 0L, 2L, 
#>     0L, 0L, 0L), Leech4 = c(0L, 0L, 0L, 0L, 0L, 0L), Leuco = c(0L, 
#>     0L, 0L, 0L, 0L, 0L), Limoniindet = c(0L, 0L, 0L, 0L, 0L, 
#>     0L), Lumbri = c(0L, 0L, 0L, 0L, 0L, 0L), Lymnae = c(0L, 0L, 
#>     0L, 0L, 0L, 0L), Maya = c(0L, 0L, 0L, 0L, 0L, 0L), Molo1 = c(1L, 
#>     0L, 0L, 0L, 0L, 0L), Molo2 = c(0L, 0L, 0L, 0L, 1L, 0L), Morto = c(0L, 
#>     0L, 0L, 0L, 0L, 0L), Musci5 = c(0L, 0L, 0L, 0L, 0L, 0L), 
#>     Musci6 = c(0L, 0L, 0L, 0L, 0L, 0L), Musci7 = c(0L, 0L, 0L, 
#>     0L, 0L, 0L), Musci4 = c(1L, 0L, 0L, 1L, 0L, 0L), Musci1 = c(11L, 
#>     0L, 8L, 0L, 2L, 0L), Musci2 = c(0L, 0L, 0L, 0L, 0L, 0L), 
#>     Musci3 = c(0L, 0L, 0L, 0L, 0L, 0L), Naid1 = c(39L, 0L, 32L, 
#>     0L, 0L, 0L), Naid2 = c(0L, 0L, 0L, 0L, 0L, 0L), Naid3 = c(0L, 
#>     0L, 0L, 0L, 0L, 0L), Nectop = c(0L, 0L, 0L, 0L, 0L, 0L), 
#>     Nema = c(2L, 0L, 4L, 0L, 2L, 0L), Neoel1 = c(0L, 0L, 0L, 
#>     0L, 0L, 0L), Neoel2 = c(0L, 0L, 1L, 0L, 0L, 0L), Neot = c(0L, 
#>     0L, 0L, 0L, 0L, 0L), Nepti = c(0L, 0L, 0L, 0L, 0L, 0L), Nona1 = c(0L, 
#>     0L, 0L, 0L, 0L, 0L), Ochro = c(0L, 0L, 0L, 0L, 0L, 0L), Ortho = c(72L, 
#>     0L, 85L, 7L, 43L, 2L), Ostra = c(0L, 0L, 0L, 0L, 0L, 0L), 
#>     Podo1 = c(24L, 16L, 177L, 13L, 46L, 49L), Podo2 = c(0L, 0L, 
#>     0L, 0L, 0L, 0L), Podo3 = c(4L, 28L, 2L, 71L, 3L, 20L), Podo4 = c(1L, 
#>     0L, 2L, 1L, 0L, 14L), Priono1 = c(0L, 0L, 0L, 0L, 0L, 0L), 
#>     Priono2 = c(0L, 0L, 0L, 0L, 0L, 0L), Priono3 = c(0L, 0L, 
#>     0L, 0L, 0L, 0L), Schoe = c(0L, 0L, 0L, 0L, 0L, 0L), Simu1 = c(124L, 
#>     0L, 18L, 0L, 3L, 0L), Sphae = c(0L, 0L, 0L, 0L, 0L, 0L), 
#>     Staphy1 = c(1L, 0L, 0L, 0L, 0L, 1L), Staphy2 = c(0L, 0L, 
#>     0L, 0L, 0L, 0L), Stilo1 = c(0L, 0L, 0L, 0L, 0L, 0L), Sync1 = c(0L, 
#>     0L, 0L, 0L, 0L, 0L), Tanyp = c(0L, 0L, 0L, 0L, 0L, 0L), Tipu = c(0L, 
#>     0L, 0L, 0L, 0L, 0L), Trichoco = c(0L, 0L, 0L, 0L, 0L, 0L)), row.names = c(2L, 
#> 3L, 4L, 5L, 8L, 9L), class = "data.frame")

Created on 2020-03-24 by the reprex package (v0.3.0)

  • Welcome to Stack Overflow Rodrigo, could you provide some data? And when you do `spe[,-1]` you are taking off the first column of your data.frame do you want remove this column in the count/sum? – vpz Mar 24 '20 at 17:37
  • Welcome to Stack Overflow! Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Mar 24 '20 at 17:39
  • Hello https://stackoverflow.com/users/7723965/vpz, yes, I want to avoid my first column because those are the numbers of my sites. – Rodrigo Espinosa Barrera Mar 24 '20 at 18:09
  • Since I'm new to SO I've added a pic of the results I had to my question, but I'll try to share a sample of my data as https://stackoverflow.com/users/786542/tung have suggested me to do. Thanks. – Rodrigo Espinosa Barrera Mar 24 '20 at 18:24
  • Now I've added a reprex of my issue – Rodrigo Espinosa Barrera Mar 24 '20 at 18:54
  • Dave2e probably you meant that my df do not include one column? Well, the first column are the numbers of my sites, so I don't want them to be included. I exported my df with the function `write_xlsx()` and there are all my rows and columns and the sum/count still vary from those obtained in R. – Rodrigo Espinosa Barrera Mar 24 '20 at 20:34
  • I was referring to the seventh row of data. For the first six rows, (the sample posted) your script worked as expected. It was the 12th and 28th rows that didn't work. – Dave2e Mar 25 '20 at 12:42
  • Yes, probably the problem was that I didn’t have a name for my sites on the first column, and because I put in the `read.table` function `row.names=1`, when I edited those issues the sums/counts worked fine. Thanks – Rodrigo Espinosa Barrera Mar 26 '20 at 19:10

0 Answers0