计算数据框中某些列的方差

时间:2018-11-30 16:00:32

标签: r dplyr purrr

我正在尝试使用purrr::pmapbase::var从数据帧计算每行三个变量的方差。

可复制的示例:

library(dplyr)
library(purrr)

set.seet(123)

df <- data.frame(a = rnorm(10, 100, 5),
                 b = rnorm(10, 100, 5),
                 c = rnorm(10, 100, 5), 
                 d = "abc",
                 e = "cmc")

df <- df %>% 
  mutate(vars_var = pmap(list(a,b,c), ~ var(c(..1, ..2, ..3))))
df

不幸的是,这导致所有值均为NA。有人可以指出正确的方向

2 个答案:

答案 0 :(得分:1)

我们可以使用c连接到vector,然后应用var

df %>% 
   mutate(vars_var = pmap_dbl(list(a,b,c), ~ var(c(...))))
#            a         b         c   d   e  vars_var
#1  107.99999 103.63304 102.34089 abc cmc  8.794236
#2   95.82441 101.63287  96.35934 abc cmc 10.305745
#3  105.78436  99.18291  99.01016 abc cmc 14.916436
#4   93.40284 103.69875 104.54950 abc cmc 38.496276
#5  109.37737  98.79907 104.43685 abc cmc 28.015604
#6  100.34486  96.36210 101.65905 abc cmc  7.607847
#7   99.64536 105.58825 100.36501 abc cmc 10.519687
#8  100.34628  99.93914 110.02011 abc cmc 32.562415
#9  103.09767 101.59083  96.83285 abc cmc 10.692810
#10 100.27754 102.85691 101.83375 abc cmc  1.686977

如果我们使用OP的方法,请在串联后使用unlist,因为它仍然是list

df %>%
   mutate(vars_var = pmap(list(a, b, c), ~ var(unlist(c(..1, ..2, ..3)))))

答案 1 :(得分:1)

或将100%移至基础 public function getReset($token = null) { $view = 'auth.web.reset2'; if ( is_null( $token ) ) { throw new NotFoundHttpException; } $passwordReset = PasswordReset::getEmailFromToken( $token ); if ( is_null( $passwordReset ) ) { throw new NotFoundHttpException; } $user = User::getUserFromEmail( $passwordReset->email ); if ( is_null( $user ) ) { throw new NotFoundHttpException; } if(User::isUserMobileClient($user)){ //$view = 'auth.reset'; } $strings = array( trans(Strings::PASSWORD_RESET_BLADE_01), trans(Strings::PASSWORD_RESET_BLADE_02), trans(Strings::PASSWORD_RESET_BLADE_03), trans(Strings::PASSWORD_RESET_BLADE_04), trans(Strings::PASSWORD_RESET_BLADE_05), trans(Strings::PASSWORD_RESET_BLADE_06), trans(Strings::PASSWORD_RESET_BLADE_051), trans(Strings::PASSWORD_RESET_BLADE_07), trans(Strings::PASSWORD_RESET_BLADE_08), ); return view($view,['strings'=>$strings, 'email'=>$passwordReset->email, 'token' => $token]); }

 public function postReset(Request $request)
{
    $userFromMobile = false;

    $this->validate($request, [
        'token' => 'required',
        'email' => 'required|email',
        'password' => 'required|confirmed|min:6',
    ]);

    $credentials = $request->only(
        'email', 'password', 'password_confirmation', 'token'
    );

    $response = Password::reset($credentials, function ($user, $password) {
        $this->user;
        $this->resetPassword($user, $password);
    });

    if(User::isUserMobileClient(User::getUserFromgEmail($request->email))){
        $userFromMobile = true;
    }

    switch ($response) {
        case Password::PASSWORD_RESET:

            Input::flashonly('status');

            return ($userFromMobile) ?
                redirect('password/changed')->with('status', trans($response)) :
                redirect($this->redirectPath())->with('status', trans($response));

        default:
            return redirect()->back()
                ->withInput($request->only('email'))
                ->withErrors(['email' => trans($response)]);
    }
}