平均标准化变量并添加到数据帧

时间:2018-05-16 17:31:57

标签: r data-manipulation

我想标准化数据集中的几个变量(表示= 0和标准差= 1),然后将这些变量添加到带有后缀' _sd'的数据框中。添加到变量名称。我曾尝试编辑下面的日志转换代码但是没有用。谢谢!

select MID, DID, DiagnosisCategoryId as CID
from
(
    select MemberId as MID, min(DiagnosisId) as DID
    from MemberDiagnosis
    group by MemberId
) src
inner join DiagnosisCategoryMap dcm
    on dcm.DiagnosisId = src.DID

2 个答案:

答案 0 :(得分:2)

这似乎对我有用:

dat <- mtcars %>%
  mutate_at(vars(disp, hp, wt, qsec), funs(sd =  (. - mean(.)) / sd(.)))

答案 1 :(得分:0)

试试这个

log.outcomes <- c("disp", "hp", "wt", "qsec")
data <- mtcars %>%
           mutate_at(vars(one_of(log.outcomes)), funs(sd = scale(., center=TRUE, scale=TRUE)))