向量中的每个元素乘以下一个元素[r]

时间:2020-09-30 00:27:21

标签: r

 use Illuminate\Support\Facades\Route;
 // This file is here so the backpack package will see that,
 // and load this file, instead of the one in the package



 /*
 |--------------------------------------------------------------------------
 | Backpack\Base Routes
 |--------------------------------------------------------------------------
 |
 | This file is where you may define all of the routes that are
 | handled by the Backpack\Base package.
 |
 */

 Route::group([
     'namespace'  => 'Backpack\CRUD\app\Http\Controllers',
     'middleware' => config('backpack.base.web_middleware', 'web'),
     'prefix'     => config('backpack.base.route_prefix'),
 ], function () {
     // if not otherwise configured, setup the auth routes
     if (config('backpack.base.setup_auth_routes')) {
         // Authentication Routes...
         Route::get('login', 'Auth\LoginController@showLoginForm')->name('backpack.auth.login');
        // Route::post('login', 'Auth\LoginController@login');  // COMMENT OUT THIS LINE HERE
         Route::get('logout', 'Auth\LoginController@logout')->name('backpack.auth.logout');
         Route::post('logout', 'Auth\LoginController@logout');

         // Registration Routes...
         Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('backpack.auth.register');
         Route::post('register', 'Auth\RegisterController@register');

         // Password Reset Routes...
         Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('backpack.auth.password.reset');
         Route::post('password/reset', 'Auth\ResetPasswordController@reset');
         Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('backpack.auth.password.reset.token');
         Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('backpack.auth.password.email');
     }

     // if not otherwise configured, setup the dashboard routes
     if (config('backpack.base.setup_dashboard_routes')) {
         Route::get('dashboard', 'AdminController@dashboard')->name('backpack.dashboard');
         Route::get('/', 'AdminController@redirect')->name('backpack');
     }

     // if not otherwise configured, setup the "my account" routes
     if (config('backpack.base.setup_my_account_routes')) {
         Route::get('edit-account-info', 'MyAccountController@getAccountInfoForm')->name('backpack.account.info');
         Route::post('edit-account-info', 'MyAccountController@postAccountInfoForm')->name('backpack.account.info.store');
         Route::post('change-password', 'MyAccountController@postChangePasswordForm')->name('backpack.account.password');
     }
 });

 // Load our custom login route
 Route::group([
     'prefix'     => config('backpack.base.route_prefix', 'admin'),
     'middleware' => config('backpack.base.web_middleware', 'web'),
     'namespace'  => 'App\Http\Controllers\Admin',
 ], static function () {
     // Authentication Routes...
     Route::post('login', 'Auth\LoginController@login');
 });

我试图找到累积的利率,为此我需要在每个利率中加1(我已经做过并将其保存到矢量对象中),然后将每个利率乘以下一个{ 1}}的结果,如何将向量的第一个元素乘以下一个,等等?

2 个答案:

答案 0 :(得分:1)

如果我们需要返回长度相同的cumprod,并且每个值乘以先前的乘积值,则可以使用vector

cumprod(vector-1)

如果我们需要退货,请使用prod

prod(vector-1)

或者使用Reduce*

Reduce(`*`, vector-1)

答案 1 :(得分:1)

您还可以使用for循环:

v <- c(1.007469, 1.007469, 1.007369, 1.007419, 1.007269, 1.007769, 1.007969, 1.007469, 1.007469 ,1.007469, 1.007469, 1.007469, 1.004469, 1.007469, 1.007469, 1.006469, 1.007469 ,1.007469 ,1.007469 ,1.007469 ,1.007869 ,1.007469 ,1.007469 ,1.007469 ,1.007469, 1.007469,
 1.007469, 1.007469 ,1.007469, 1.007469)

res <- -100
for(i in 1:length(v)) {
    res <- res * v[i]
}
print(res)

cumulative_v = -100 * cumprod(v)
stopifnot(all.equal(res, cumulative_v[length(cumulative_v)]))

输出:

[1] -124.6208