Note that there are some explanatory texts on larger screens.

plurals
  1. POAvoiding Loop with R using Apply (?)
    primarykey
    data
    text
    <p>I'm trying to run apply a function to each row of a dataset. The function looks up matching rows in a second dataset and computes a similarity score for the product details passed to it.</p> <p>The function works if I just call it with test numbers but I can't figure out how to run it on all rows of my dataset. I've tried using apply but can't get it working.</p> <p>I'm going to be iterating different parameter settings to find those that best fit historical data so speed is important... meaning that a loop is out. Any help you can provide would be hugely appreciated.</p> <p>Thanks! Alan</p> <pre><code>GetDistanceTest &lt;- function(SnapshotDate, Cand_Type, Cand_Height, Cand_Age) { HeightParam &lt;- 1/5000 AgeParam &lt;- 1 Stock_SameType &lt;- HistoricalStock[!is.na(HistoricalStock$date) &amp; !is.na(HistoricalStock$Type) &amp; as.character(HistoricalStock$date)==as.character(SnapshotDate) &amp; HistoricalStock$Type==Cand_Type,] Stock_SameType$ED &lt;- (HeightParam*(Stock_SameType$Height - Cand_Height))^2 + (AgeParam*(Stock_SameType$Age - Cand_Age))^2 return(sqrt(sum(Stock_SameType$ED))) } HistoricalStock &lt;- HistoricalAQStock[,c(1, 3, 4, 5)] colnames(HistoricalStock) &lt;- c("date", "Age", "Height", "Type") Sales &lt;- AllSales[,c(2,10,11,25)] colnames(Sales) &lt;- c("date", "Age", "Height", "Type") GetDistanceTest("2010-04-01", 5261, 12, 7523) #works and returns a single number res1 &lt;- transform(Sales, ClusterScore=GetDistanceTest(date, Type, Height, Age)) # returns Error in `$&lt;-.data.frame`(`*tmp*`, "ED", value = c(419776714.528591, 22321257.0276852, : replacement has 4060 rows, data has 54 # also 4 warnings, one for each variable. e.g. 1: In as.character(HistoricalStock$date) == as.character(SnapshotDate) : longer object length is not a multiple of shorter object length res2 &lt;- apply(Sales, 1, GetDistanceTest, Sales$Type, Sales$Height, Sales$Age) # `$&lt;-.data.frame`(`*tmp*`, "ED", value = c(419648071.041523, 22325941.2704261, : replacement has 4060 rows, data has 13 # also same 4 warnings as res1 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload