我编写了3个函数,这些函数都做同样的事情,我想知道为什么当我对它们进行微基准测试时,我认为这个函数将是最快的。下面是我的代码,我只是想知道是否有人可以帮助解释为什么mymean是最快的。
#First remove all non numeric columns
numeric_clean_usnews <- cleaned_us_news[,-c((1:3),36)] #Removing any non numeric columns
#using apply and mean()
Average_1 <-apply(numeric_clean_usnews,2,mean,na.rm=T)
Average_1
#using apply and user created mean function
mymean <- function(cleaned_us){
column_total = sum(cleaned_us,na.rm=T)
column_length = sum(!is.na(cleaned_us))
return (column_total/column_length)
}
Average_2 <- apply(numeric_clean_usnews,2,mymean)
Average_2
#using 2 for loops
mymean2 <- function (cleaned_usnews){
column_averages = numeric(dim(cleaned_us_news_only_numeric)[2])
for (column in 1:ncol(cleaned_us_news_only_numeric)){
column_total = 0
column_length = 0
for (row in 1:nrow(cleaned_us_news_only_numeric)){
if(!is.na(cleaned_us_news_only_numeric[row,column])){
column_total = column_total + cleaned_us_news_only_numeric[row,column]
column_length= column_length + 1
}
}
column_averages[column]=column_total/column_length
}
return (column_averages)
}
Average_3 <- mymean2(numeric_clean_usnews)