通过向量值获取列号 - R
Getting column number by vector value - R
我被这个问题困了有一段时间了,没有在网上找到任何帮助。
我有两个包含 1.314.000 个条目的向量,一个表示风速,一个表示风向(值是 1-12,因为方向合并为 12 个方向)。
我还有一个 50001 行和 13 列的矩阵,表示在给定风速(第 1 列)和风向(第 2-13 列)下涡轮机的发电量
计算发电量似乎很明显:
PowerkWh <- PCC[ws*1000+1,1+wd]
PCC是50001x13矩阵,ws和wd是风速风向变量。请注意,我不使用匹配,因为出于某种原因它导致了 NA。
ws、wd 和 PCC 负责人:
ws:
[1] 2.327 16.971 3.469 23.558 7.882 10.619
wd:
[1] 5 10 10 9 1 1
PCC:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
[1,] 0.000 0 0 0 0 0 0 0 0 0 0 0 0
[2,] 0.001 0 0 0 0 0 0 0 0 0 0 0 0
[3,] 0.002 0 0 0 0 0 0 0 0 0 0 0 0
[4,] 0.003 0 0 0 0 0 0 0 0 0 0 0 0
[5,] 0.004 0 0 0 0 0 0 0 0 0 0 0 0
[6,] 0.005 0 0 0 0 0 0 0 0 0 0 0 0
请注意,PCC 为 0,因为风力涡轮机通常仅在风速高于 3.5 时才启动 m/s
此代码可以很好地获取正确的行(设置常量 column/direction,比如 5),但是我的问题是获取适当的列,给定 wd 的风向。 R 简单地输出它的内存已经达到,因为在将它解释为 1.314.000x1.314.000 矩阵 - 我期待一个 1.314.000 向量与给定风速和风向的产生。
希望您能看到我不了解的内容,因为我不想因为耗时而使用循环 - 如果您需要更多信息,请告诉我:)
谢谢!
我的回答假设 ws
中风速的单位与 PCC
第一列中的单位相同。
# ===============================
# = Quick Options for Fake Data =
# ===============================
n.dir <- 12 # number of wind directions
n.sample <- 50 # number of samples in the vectors
# ================================
# = Create Fake Data for Example =
# ================================
# create fake vectors and lookup tables
ws <- rlnorm(n.sample) # wind speeds
wd <- sample(1:n.dir, n.sample, replace=T) # wind directions
PCC <- matrix( # empty lookup table for power
data=NA,
nrow=length(unique(ceiling(ws)))+1,
ncol=length(wd)+1
)
# note the +1 is so that the top row of PCC
# references values that are closest to 0;
# i.e., the starting row of the lookup matrix
# references the lowest possible wind speed
# generate fake power values;
# absolute values to make realistic
# data are also sorted so that more wind is more power
# beyond that, it is not realistic
dataExprsn <- expression(sort(abs(rnorm(nrow(PCC)))))
PCC[] <- replicate(ncol(PCC), eval(dataExprsn))
PCC[,1] <- c(0,sort(unique(ceiling(ws))))
# =============================
# = Approach to be Used by OP =
# =============================
power <- rep(NA, length=length(ws)) # to store output
for(i in 1:n.dir){ # do it by direction to save time
t.index <- wd==i
t.fun <- approxfun(x=PCC[,1], y=PCC[,i+1])
power[t.index] <- t.fun(ws[t.index])
}
# ==============
# = The Answer =
# ==============
power
您可以尝试 - 如果 PCC
的第一列包含 ws
的所有元素:
PCC[cbind(which(PCC[,1] %in% ws), wd + 1)]
我被这个问题困了有一段时间了,没有在网上找到任何帮助。
我有两个包含 1.314.000 个条目的向量,一个表示风速,一个表示风向(值是 1-12,因为方向合并为 12 个方向)。 我还有一个 50001 行和 13 列的矩阵,表示在给定风速(第 1 列)和风向(第 2-13 列)下涡轮机的发电量
计算发电量似乎很明显:
PowerkWh <- PCC[ws*1000+1,1+wd]
PCC是50001x13矩阵,ws和wd是风速风向变量。请注意,我不使用匹配,因为出于某种原因它导致了 NA。
ws、wd 和 PCC 负责人:
ws:
[1] 2.327 16.971 3.469 23.558 7.882 10.619
wd:
[1] 5 10 10 9 1 1
PCC:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
[1,] 0.000 0 0 0 0 0 0 0 0 0 0 0 0
[2,] 0.001 0 0 0 0 0 0 0 0 0 0 0 0
[3,] 0.002 0 0 0 0 0 0 0 0 0 0 0 0
[4,] 0.003 0 0 0 0 0 0 0 0 0 0 0 0
[5,] 0.004 0 0 0 0 0 0 0 0 0 0 0 0
[6,] 0.005 0 0 0 0 0 0 0 0 0 0 0 0
请注意,PCC 为 0,因为风力涡轮机通常仅在风速高于 3.5 时才启动 m/s
此代码可以很好地获取正确的行(设置常量 column/direction,比如 5),但是我的问题是获取适当的列,给定 wd 的风向。 R 简单地输出它的内存已经达到,因为在将它解释为 1.314.000x1.314.000 矩阵 - 我期待一个 1.314.000 向量与给定风速和风向的产生。
希望您能看到我不了解的内容,因为我不想因为耗时而使用循环 - 如果您需要更多信息,请告诉我:)
谢谢!
我的回答假设 ws
中风速的单位与 PCC
第一列中的单位相同。
# ===============================
# = Quick Options for Fake Data =
# ===============================
n.dir <- 12 # number of wind directions
n.sample <- 50 # number of samples in the vectors
# ================================
# = Create Fake Data for Example =
# ================================
# create fake vectors and lookup tables
ws <- rlnorm(n.sample) # wind speeds
wd <- sample(1:n.dir, n.sample, replace=T) # wind directions
PCC <- matrix( # empty lookup table for power
data=NA,
nrow=length(unique(ceiling(ws)))+1,
ncol=length(wd)+1
)
# note the +1 is so that the top row of PCC
# references values that are closest to 0;
# i.e., the starting row of the lookup matrix
# references the lowest possible wind speed
# generate fake power values;
# absolute values to make realistic
# data are also sorted so that more wind is more power
# beyond that, it is not realistic
dataExprsn <- expression(sort(abs(rnorm(nrow(PCC)))))
PCC[] <- replicate(ncol(PCC), eval(dataExprsn))
PCC[,1] <- c(0,sort(unique(ceiling(ws))))
# =============================
# = Approach to be Used by OP =
# =============================
power <- rep(NA, length=length(ws)) # to store output
for(i in 1:n.dir){ # do it by direction to save time
t.index <- wd==i
t.fun <- approxfun(x=PCC[,1], y=PCC[,i+1])
power[t.index] <- t.fun(ws[t.index])
}
# ==============
# = The Answer =
# ==============
power
您可以尝试 - 如果 PCC
的第一列包含 ws
的所有元素:
PCC[cbind(which(PCC[,1] %in% ws), wd + 1)]