Ошибка в FUN(X[[i]], …) : недопустимый «тип» (символ) аргумента с ударениями

#r #xtabs

#r #xtabs

Вопрос:

У меня возникли проблемы с загрузкой моих данных. Если кто-нибудь сможет взглянуть, я был бы вам очень признателен!

Код

  data <- structure(list(Date = c("2-Nov-20", "2-Nov-20", "2-Nov-20", "2-Nov-20", 
"2-Nov-20", "2-Nov-20"), Cycle = c(1L, 1L, 1L, 1L, 1L, 1L), Route = c("T1", 
"T1", "T1", "T1", "T1", "T1"), Waypoint = c("FQ1120", "FQ1121", 
"FQ1122", "FQ1123", "FQ1127", "FQ1125"), Latitude = c("1.326983012", 
"1.327218041", "1.327946009", "1.328284973", "1.329542007", "1.329018977"
), Longitude = c("103.659741", "103.659496", "103.659467", "103.65963", 
"103.660734", "103.659631"), Sampling.point = c("T01_01", "T01_01", 
"T01_01", "T01_02", "T01_20", "T01_02"), Latitude.1 = c(NA, NA, 
NA, NA, NA, NA), Longitude.1 = c(NA, NA, NA, NA, NA, NA), Time..24h. = c("1947", 
"1948", "1950", "1952", "2003", "1957"), Common.name = c("Wild pig", 
"Red junglefowl", "Changeable lizard", "Savanna nightjar", "Changeable lizard", 
"Yellow-vented bulbul"), Taxon = c("Mammal", "Bird", "Reptile", 
"Bird", "Reptile", "Bird"), Scientific.name = c("Sus scrofa", 
"Gallus gallus", "Calotes versicolor", "Caprimulgus affinis", 
"Calotes versicolor", "Pycnonotus goiavier"), Global.status..IUCN.CITES. = c("Least Concern", 
"Least Concern", "Not Assessed", "Least Concern", "Not Assessed", 
"Least Concern"), Local.status..Davison.et.al...2008..Jain.et.al...2018.for.butterflies..Soh.et.al...2019.for.odonates. = c("Not Assessed", 
"Endangered", "Not Assessed", "Not Assessed", "Not Assessed", 
"Not Assessed"), Quantity = c("1", "1", "1", "1", "3", "7"), 
    Observation.type..seen.heard.caught.scat.other.signs. = c("Seen", 
    "Seen", "Seen", "Heard", "Seen", "Seen"), Photo.no. = c("", 
    "", "", "", "", ""), Survey.method..targeted.incidental.point.count.trapping. = c("Incidental", 
    "Incidental", "Targeted", "Targeted", "Targeted", "Targeted"
    ), Remarks = c("", "", "", "", "", ""), abundance = c(1, 
    1, 1, 1, 1, 1)), row.names = c(NA, 6L), class = "data.frame")
    
    
    data$abundance <- 1 #add a column of 1
data.matrix <- xtabs(Quantity~Scientific.name Sampling.point, data=data) 
 

Код ошибки:

 Error in FUN(X[[i]], ...) : invalid 'type' (character) of argument
 

не знаете, почему это происходит? любая помощь приветствуется!

Ответ №1:

Нам нужно, чтобы «Количество» было числовым. Это character класс

 data$Quantity <- as.numeric(data$Quantity)
xtabs(Quantity~Scientific.name Sampling.point, data=data) 
#                     Sampling.point
#Scientific.name       T01_01 T01_02 T01_20
#  Calotes versicolor       1      0      3
#  Caprimulgus affinis      0      1      0
#  Gallus gallus            1      0      0
#  Pycnonotus goiavier      0      7      0
#  Sus scrofa               1      0      0