How to calculate the average of a value in R in a loop -
i have dataset one:
date_a date_b type price 2345 2400 120 1115 1230 b 226 930 945 c 90 1050 1100 157
and want print values type column values of date_a less date_b. part did following code:
for(i in 1:length(list)){ if(`date_a`[i]<`date_b`[i]){ print(type[i]) } }
this code works fine. want calculate total average of price column type values printed before type's date_a < date_b. thought of doing like:
for(i in 1:length(list)){ if(`date_a`[i]<`date_b`[i]){ print(type[i]) price_average[i] <- mean(price) }
however gives me total mean of whole dataset , want mean of price date_a < date_b. trying figure out how calculate using loops since practicing loops in r.
i'm not sure understand want. try this:
mean(price[date_a < date_b])
wiki
Comments
Post a Comment