Home > Net >  cannot coerce class ‘"by"’ to a data.frame (R)
cannot coerce class ‘"by"’ to a data.frame (R)

Time:10-05

I have the given lists and vector :

Spec <- c("Calanus finmarchicus", "Calanus glacialis", "Calanus hyperboreus", 
"Calanus spp.", "Copepoda", "Metridia longa", "Microcalanus pusillus", 
"Microcalanus spp.", "Microsetella norvegica", "Oithona similis", 
"Oithona spp.", "Oncaea borealis", "Pseudocalanus spp.", "Microcalanus pygmaeus", 
"Pareuchaeta glacialis", "Calanoida indet.", "Oithona atlantica", 
"Pseudocalanus minutus", "Pareuchaeta spp.", "Acartia spp.")
ZoodataL50 <- structure(list(Transect = c("1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "4", "4", "4", "4", "4", 
"4", "4", "4", "4", "4", "4", "4", "4"), Station = c("1", "1", 
"1", "1", "1", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"
), Species = c("Calanus finmarchicus", "Calanus glacialis", "Calanus hyperboreus", 
"Calanus spp.", "Pareuchaeta glacialis", "Calanus finmarchicus", 
"Calanus glacialis", "Calanus hyperboreus", "Calanus spp.", "Calanus finmarchicus", 
"Calanus glacialis", "Calanus hyperboreus", "Calanus finmarchicus", 
"Calanus glacialis", "Calanus hyperboreus", "Calanus finmarchicus", 
"Calanus glacialis", "Microcalanus spp.", "Calanus finmarchicus", 
"Calanus glacialis", "Calanus hyperboreus", "Calanus spp.", "Oncaea borealis", 
"Pseudocalanus spp.", "Calanus finmarchicus", "Calanus glacialis", 
"Calanus hyperboreus", "Microcalanus spp."), mean_turb = c(51.7, 
51.7, 51.7, 51.7, 51.7, 15, 15, 15, 15, 185.1, 185.1, 185.1, 
293.7, 293.7, 293.7, 2.1, 2.1, 2.1, 3.6, 3.6, 3.6, 3.6, 3.6, 
3.6, 14.9, 14.9, 14.9, 14.9), Rel_biom = c(0.425798412200999, 
0.0894303667726106, 0.0982034104785712, 0.0121735930667325, 0.0289298177369833, 
0.691934975888281, 0.177489780335863, 0.106622503989315, 0.25405181232058, 
0.349900172337648, 0.444028340606541, 0.116759738674243, 0.191825202881111, 
0.077277494854345, 0.102549858501126, 0.00681106641422225, 0.00613391314191942, 
0.00612500553647714, 0.026707131460647, 0.0371160659539919, 0.0367832608206733, 
0.00461676295740788, 0.0103411034459642, 0.0079667991705717, 
0.0150774967105538, 0.0350675898070042, 0.0227696283037591, 0.00261294954249131
), frequency = c(0.509876294828541, 0.107089229899832, 0.117594593214932, 
0.0145773829816114, 0.034642281077448, 0.214709581947223, 0.0550756326299232, 
0.0330852956642681, 0.0788330700384962, 0.108575115211415, 0.137783379517648, 
0.0362309112165206, 0.0595239589741372, 0.0239794477693581, 0.0318215410620878, 
0.0293020150757093, 0.0263888214307096, 0.0263504998562182, 0.114897245320459, 
0.159677715352168, 0.15824594821899, 0.019861861498878, 0.0444886528254054, 
0.0342741143903377, 0.0648651780863677, 0.150864928148107, 0.0979576400006712, 
0.0112412186623629)), row.names = c(NA, -28L), class = c("tbl_df", 
"tbl", "data.frame"))

Ll <- structure(list(`1 1` = c("Copepoda", "Metridia longa", "Microcalanus pusillus", 
"Microcalanus spp.", "Microsetella norvegica", "Oithona similis", 
"Oithona spp.", "Oncaea borealis", "Pseudocalanus spp.", "Microcalanus pygmaeus", 
"Calanoida indet.", "Oithona atlantica", "Pseudocalanus minutus", 
"Pareuchaeta spp.", "Acartia spp."), `1 2` = c("Copepoda", "Metridia longa", 
"Microcalanus pusillus", "Microcalanus spp.", "Microsetella norvegica", 
"Oithona similis", "Oithona spp.", "Oncaea borealis", "Pseudocalanus spp.", 
"Microcalanus pygmaeus", "Pareuchaeta glacialis", "Calanoida indet.", 
"Oithona atlantica", "Pseudocalanus minutus", "Pareuchaeta spp.", 
"Acartia spp."), `4 1` = c("Copepoda", "Metridia longa", "Microcalanus pusillus", 
"Microsetella norvegica", "Oithona similis", "Oithona spp.", 
"Microcalanus pygmaeus", "Pareuchaeta glacialis", "Calanoida indet.", 
"Oithona atlantica", "Pseudocalanus minutus", "Pareuchaeta spp.", 
"Acartia spp.")), .Dim = 3L, .Dimnames = list(`Reduce(paste, ZoodataL50[1:2])` = c("1 1", 
"1 2", "4 1")), call = by.default(data = ZoodataL50$Species, 
    INDICES = Reduce(paste, ZoodataL50[1:2]), FUN = setdiff, 
    x = Spec), class = "by")

And I would like to do have the list Ll in data frame in order to do a pivot_longer() with the different columns. Nevertheless, when I try to apply that code Ll <- as.data.frame(Ll), I obtain the error Error in as.data.frame.default(Sp_Biom_L1Turb) : cannot coerce class ‘"by"’ to a data.frame.

Then I have tried to transform it first in matrix before with Ll <- as.matrix(as.data.frame(Ll)). But the result is the same.

CodePudding user response:

We could use base R

out <- data.frame(ID = rep(names(Ll), lengths(Ll)), Col1 = unlist(Ll))
row.names(out) <- NULL

-output

> head(out)
   ID                   Col1
1 1 1               Copepoda
2 1 1         Metridia longa
3 1 1  Microcalanus pusillus
4 1 1      Microcalanus spp.
5 1 1 Microsetella norvegica
6 1 1        Oithona similis

Or with tidyverse

library(tibble)
library(purrr)
map_dfr(Ll, enframe, .id = 'ID')

-output

# A tibble: 44 × 3
   ID     name value                 
   <chr> <int> <chr>                 
 1 1 1       1 Copepoda              
 2 1 1       2 Metridia longa        
 3 1 1       3 Microcalanus pusillus 
 4 1 1       4 Microcalanus spp.     
 5 1 1       5 Microsetella norvegica
 6 1 1       6 Oithona similis       
 7 1 1       7 Oithona spp.          
 8 1 1       8 Oncaea borealis       
 9 1 1       9 Pseudocalanus spp.    
10 1 1      10 Microcalanus pygmaeus 
# … with 34 more rows
  • Related