我有一个数据框和一个矢量:
parameters <- data.frame(index = c(3:12, 15:18, 23:25), variable = c("Pin",
"Pout", "Tin", "speed", "D", "L", "Cin_h", "Cout_h", "VdA",
"preswirl", "mu", "mol_weight", "Cp", "Z", "ffactor_N",
"ffactor_M", "nfreqs"),
value = c(65, 4, 16.85, 7900, 110, 60, 0.1975, .1875, 2.31,
0.2, 0.0011877, 22.0, 1.4, 1.0, 0.0785, -0.1101,
30))
temp <- runif(100)
我想从parameters
读取一个索引向量,并将它们用作索引向量来读入temp
。例如,我们假设我希望索引对应变量nfreqs
和Pout
:
library(dplyr)
index_vector <- parameters %>% filter(variable %in% c("Pout","nfreqs")) %>% select(index)
问题是现在index_vector
不是矢量,而是数据框,因此我无法使用它来读取temp
:
> temp[index_vector]
Error in temp[index_vector] : invalid subscript type 'list'
temp[index_vector$index]
当然有效。但我想知道我是否可以在dplyr
调用期间直接提取我感兴趣的向量,即通过适当修改
index_vector <- parameters %>% filter(variable %in% c("Pout","nfreqs")) %>% select(index)