Demean R 数据框 [英] Demean R data frame

查看:55
本文介绍了Demean R 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想贬低 R data.frame 中的多列.使用 这个问题中的示例

I would like to demean multiple columns in an R data.frame. Using an example from this question

set.seed(999)
library(plyr)
library(plm)
# random data.frame
dat <- expand.grid(id=factor(1:3), cluster=factor(1:6))
dat <- cbind(dat, x=runif(18), y=runif(18, 2, 5))

#demean x and y
dat.2 <- ddply(dat, .(cluster), transform, x=x-mean(x), y=y-mean(y))

我的问题是我有(很多)两个以上的变量,我想避免对这个分析进行硬编码.总的来说,我是 plyr 的新手;为什么会这样

My problem is that I have (lots) more than 2 variables, and I would like to avoid hard-coding this analysis. I'm new to plyr in general; why does this

dat.2 <- ddply(dat[,c(x,y)],  .(cluster), transform, function(x) x - mean(x))

不工作?有没有我遗漏的关键步骤?一般有没有更好的方法来做到这一点?

not work? Is there some crucial step that I'm missing? Is there a better way to do this in general?

推荐答案

看看 colwise 函子.唯一需要注意的是 id 列.因此:

Have a look at the colwise functor. The only thing to be careful about is that id column. Hence:

demean <- colwise(function(x) if(is.numeric(x)) x - mean(x) else x)
dat.2 <- ddply(dat, .(cluster), demean)

如您所见,甚至还有一个 numcolwise 函子仅用于处理数字,因此您可以这样做:

as you found, there is even a numcolwise functor for only dealing with numerics so you can do:

demean <- numcolwise(function(x) x - mean(x))
dat.2 <- ddply(dat, .(cluster), demean)

你也可以使用 scale 函数而不是定义你自己的函数:

You can also use the scale function rather than define your own function:

dat.2 <- ddply(dat, .(cluster), numcolwise(scale, scale = FALSE))

这篇关于Demean R 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆