数据取自http://open.canada.ca/data/en/dataset/b52664cf-bfd9-49ad-849a-cb88c92553b9(英文版)
glacier <- read.csv("glacier.csv", stringsAsFactors = F)
str(glacier)
'data.frame': 518 obs. of 6 variables:
$ Ref_Date : int 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 ...
$ GEO : chr "Helm Glacier - southern Coast Mountains (Garibaldi Provincial Park), British Columbia" "Helm Glacier - southern Coast Mountains (Garibaldi Provincial Park), British Columbia" "Helm Glacier - southern Coast Mountains (Garibaldi Provincial Park), British Columbia" "Helm Glacier - southern Coast Mountains (Garibaldi Provincial Park), British Columbia" ...
$ MEASURE : chr "Annual mass balance" "Annual mass balance" "Annual mass balance" "Annual mass balance" ...
$ Vector : chr "v54326054" "v54326054" "v54326054" "v54326054" ...
$ Coordinate: num 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 ...
$ Value : chr "-1460.0" "-780.0" "-2730.0" "-940.0" ...
tapply(X = as.numeric(glacier[glacier$MEASURE == "Annual mass balance", c("Value")]),
INDEX = unique(glacier[ , 2]), FUN = median, na.rm = T)
给出错误: tapply错误(as.numeric(冰川[冰川$ MEASURE ==“年度质量平衡”,: 更改自定义更多 我检查了参数,它们对于tapply函数似乎很正常。不知道,有什么不对。提前谢谢。
编辑:
tapply(X = as.numeric(glacier[glacier$MEASURE == "Annual mass balance", c("Value")]),
INDEX = glacier[ , 2], FUN = median, na.rm = T)
给出相同的错误
答案 0 :(得分:0)
您正在尝试对冰川数据框进行子集化,我希望glacier$Measure !="Annual mass balance"
中有一个或多个元素。因此glacier[glacier$MEASURE == "Annual mass balance", c("Value")]
的列长度不等于:glacier[ , 2]
的长度。如果这是真的那么你也需要对索引进行子集化。
以下是创建数据子集以提高可读性的解决方案:
glacier <- read.csv("01530102-eng.csv", stringsAsFactors = F)
glacierreduced<-glacier[glacier$MEASURE == "Annual mass balance",]
tapply(X = as.numeric(glacierreduced$Value), INDEX = glacierreduced[ , 2],
FUN = median, na.rm = T)