- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个数据集,其中包括三种土地覆盖类型的月度ndvi和降水量数据,每种类型共有26个站点,每个站点13年。我想运行一个循环,用每种土地覆盖类型的每个站每年用“ndvi”和“cv”填充矩阵m1。最后,我需要三个环形土地覆被矩阵的三个地块。
输入以下所示的脚本时,我遇到了错误。我不确定为什么会有“不同的行数”,因为每个站点每年总是有一个最大的ndvi值和一个cv值。有人可以针对我的错误提供建议吗?该脚本用于我的均值ndvi的分析,但以某种方式使用max则不行。
dput(head(d))
structure(list(row.names = c(1L, 1769L, 2055L, 2341L, 2627L,
2913L), timestamp = 1:6, station = structure(c(1L, 1L, 1L, 1L,
1L, 1L), .Label = c("Aiselukharka", "Anarmani", "BiratnagarAirport",
"Chainpur", "Chandragadhi", "Damak", "Dhankuta", "Diktel", "Dingla",
"Haraicha", "Ilam", "Kanyam", "Kechana", "KhotangBazar", "Leguwa",
"Letang", "ManebhanjyangBazar", "Muga", "Mulghat", "Num", "Okhaldunga",
"PakhribasBazar", "Phidim", "Sanischare", "Sankhuwasabha", "Tumlingtar"
), class = "factor"), year = c(2000L, 2000L, 2000L, 2000L, 2000L,
2000L), month = structure(c(5L, 4L, 8L, 1L, 9L, 7L), .Label = c("apr",
"aug", "dec", "feb", "jan", "jul", "jun", "mar", "may", "nov",
"oct", "sept"), class = "factor"), ndvi = c(0.4138, 0.4396, 0.4393,
0.6029, 0.4756, 0.4969), landcover = structure(c(3L, 3L, 3L,
3L, 3L, 3L), .Label = c("Cropland/Natural vegetation mosaic",
"Croplands", "Mixed forest"), class = "factor"), altitude = c(2143L,
2143L, 2143L, 2143L, 2143L, 2143L), altrange = structure(c(3L,
3L, 3L, 3L, 3L, 3L), .Label = c("0-500", "1501-2000", "2001+",
"501-1500"), class = "factor"), precipitation = c(16, 4, 25.5,
72.6, 241.7, 505.9)), .Names = c("row.names", "timestamp", "station",
"year", "month", "ndvi", "landcover", "altitude", "altrange",
"precipitation"), row.names = c(NA, 6L), class = "data.frame")
d <- read.csv("asort.csv", header = TRUE, sep = ",")
stations <- levels(d$station)
landcover <- levels(d$landcover)
allyears=c$year[ ! duplicated( c$year)]
for(lc in landcover) {
m1=NULL
for(j in stations){
for (i in allyears){
tmp <- d[d$landcover==lc & d$station==j & d$year==i,]
ndvi<- tmp$ndvi[which.max(tmp$ndvi)];
precip_2m<-tmp$precipitation[tmp$month %in% c("feb","mar","apr","may","jun","jul","aug")]
cv<-sd(precip_2m,na.rm=T)/mean(precip_2m, na.rm=T)
station=j
landcover=lc
year=i
lag=l
m1 = rbind(m1, data.frame(ndvi, cv,landcover, station, year))
}
}
cat("landcover=",lc)
print(summary(aov(ndvi~cv,data=m1)))
plot(ndvi~cv,main=lc,
xlab="cv of growing season precipitation", ylab="max ndvi ", data=m1)
abline(lm(ndvi~cv, data=m1))
fit = summary(lm(ndvi~cv, data=m1))
r2 = fit$adj.r.squared
my.p = fit$coefficients[2,4]
rp = vector('expression',2)
rp[1] = substitute(expression(italic(R)^2 == value.r), list(value.r = format(r2,dig=3)))[2]
rp[2] = substitute(expression(italic(p) == value.p), list(value.p = format(my.p, digits = 2))[2]
legend('topright', legend = rp, bty = 'n')
}
Error in data.frame(ndvi, cv, landcover, station, year) :
arguments imply differing number of rows: 0, 1
最佳答案
当特定子集(nrow(tmp)==0
)中没有值时,出现该错误。 mean
和您现在正在执行的操作之间的区别是mean(NULL)
实际上返回了长度为1的 vector ,而tmp$ndvi[which.max(tmp$ndvi)]
将返回长度为零的 vector 。这是您要具体分配的值(如站号,土地覆盖物等)始终具有长度1的值与您正在计算的值(可能为零长度)之间的不匹配,因此会出现不匹配错误。
因此,您可以做两件事。最简单的是更换
ndvi<- tmp$ndvi[which.max(tmp$ndvi)];
ndvi<- max(tmp$ndvi);
max
具有与
mean
相同的行为,只要它会返回某些内容。但这当然意味着您最终的结果中只是获得了奇怪的数据。您可以使用以下方法测试空
tmp
data.fames的替代方法
for (i in allyears){
tmp <- d[d$landcover==lc & d$station==j & d$year==i,]
if(nrow(tmp)>0) {
...
m1 = rbind(m1, data.frame(ndvi, cv,landcover, station, year))
}
}
aggregate
计算大多数这些值(尽管对于不同的摘要函数,您可能不得不多次调用它)。
关于r - 循环矩阵 “arguments imply differing number of rows”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23916987/
从 angular 5.1 更新到 6.1 后,我开始从我的代码中收到一些错误,如下所示: Error: ngc compilation failed: components/forms/utils.
我正在学习 Typescript 并尝试了解类型和接口(interface)的最佳实践。我正在玩一个使用 GPS 坐标的示例,想知道一种方法是否比另一种更好。 let gps1 : number[];
type padding = [number, number, number, number] interface IPaddingProps { defaultValue?: padding
这两种格式在内存中保存结果的顺序上有什么区别吗? number = number + 10; number += 10; 我记得一种格式会立即保存结果,因此下一行代码可以使用新值,而对于另一种格式,
在 Python 匹配模式中,如何匹配像 1 这样的文字数字在按数字反向引用后 \1 ? 我尝试了 \g用于此目的的替换模式中可用的语法,但它在我的匹配模式中不起作用。 我有一个更大的问题,我想使用一
我的源文件here包含 HTML 代码,我想将电话号码更改为可在我的应用程序中单击。我正在寻找一个正则表达式来转换字符串 >numbernumber(\d+)$1numbernumber<",我们在S
我们有一个包含 2 个字段和一个按钮的表单。我们想要点击按钮来输出位于 int A 和 int B 之间的随机整数(比如 3、5 或 33)? (不需要使用 jQuery 或类似的东西) 最佳答案 你
我收到以下类型错误(TypeScript - 3.7.5)。 error TS2345: Argument of type '(priority1: number, priority2: number
只想创建简单的填充器以在其他功能中使用它: function fillLine(row, column, length, bgcolor) { var sheet = SpreadsheetApp
我有一个问题。当我保存程序输出的 *.txt 时,我得到以下信息:0.021111111111111112a118d0 以及更多的东西。 问题是: 这个数字中的“d0”和“a”是什么意思? 我不知道“
首先:抱歉标题太长了,但我发现很难用一句话来解释这个问题;)。是的,我也四处搜索(这里和谷歌),但找不到合适的答案。 所以,问题是这样的: 数字 1-15 将像这样放在金字塔中(由数组表示):
我想从字符串中提取血压。数据可能如下所示: text <- c("at 10.00 seated 132/69", "99/49", "176/109", "10.12 I 128/51, II 1
当尝试执行一个简单的 bash 脚本以将前面带有 0 的数字递增 1 时,原始数字被错误地解释。 #!/bin/bash number=0026 echo $number echo $((number
我有一个类型为 [number, number] 的字段,TypeScript 编译器(strict 设置为 true)出现问题,提示初始值值(value)。我尝试了以下方法: public shee
你能帮我表达数组吗:["232","2323","233"] 我试试这个:/^\[("\d{1,7}")|(,"\d{1,7}")\]$/ 但是这个表达式不能正常工作。 我使用 ruby(rail
这个问题在这里已经有了答案: meaning of (number) & (-number) (4 个回答) 关闭6年前. 例如: int get(int i) { int res = 0;
我正在考虑使用 Berkeley DB作为高度并发的移动应用程序后端的一部分。对于我的应用程序,使用 Queue对于他们的记录级别锁定将是理想的。但是,如标题中所述,我需要查询和更新概念建模的数据,如
我正在尝试解决涉及重复数字的特定 JavaScript 练习,为此我需要将重复数字处理到大量小数位。 目前我正在使用: function divide(numerator, denominator){
我有这个数组类型: interface Details { Name: string; URL: string; Year: number; } interface AppState {
我们正在使用 Spring 3.x.x 和 Quartz 2.x.x 实现 Web 应用程序。 Web 服务器是 Tomcat 7.x.x。我们有 3 台服务器。 Quartz 是集群式的,因此所有这
我是一名优秀的程序员,十分优秀!