[英]What's the most efficient way to partition and access dataframe rows in R?
[英]R: What is the most efficient way to select certain rows in a dataframe
在數據幀Question
,列有Question$Temperature
, Question$Salary
,我想只選擇Temperature
高於10的Salary
。我總是這樣做:
Question[Question$Temperature>10]$Salary
有更干凈的方式嗎?
它的使用效率更高
Question$Salary[Question$Temperature > 10]
因為你不是整個數據框的子集,而是矢量的值,
基准測試的三種常用方法
l<-data.frame(x=sample(1:10,1000, replace=T), y=runif(1000))
f1<-function(df){l2=df[df$x>8,"y"]}
f2<-function(df){l2=df[df$x>8,]$y}
f3<-function(df){l2=df$y[df$x>8]}
print(microbenchmark(f1(l), f2(l), f3(l), times=1000))
結果
Unit: microseconds
expr min lq median uq max neval
f1(l) 97.428 101.378 102.696 107.962 3757.555 1000
f2(l) 247.081 253.226 257.614 270.780 734.659 1000
f3(l) 59.686 62.319 63.197 64.514 3793.980 1000
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.