首页 > 解决方案 > FUN(X[[i]], ...) 中的错误:带刺的参数的“类型”(字符)无效

问题描述

我在加载数据时遇到问题。如果有人可以看看,那将不胜感激!

代码

 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

不知道为什么会这样?任何帮助表示赞赏!

标签: rxtabs

解决方案


我们需要“数量”为数字。是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

推荐阅读