1
我有一個數據幀和一向量提取索引的矢量:選擇並處理來自數據幀
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)