2

这似乎不应该太难,但我很难做到这一点。例如,我有以下数据框:

set.seed(99)
data <- data.frame(Names=rep(c('A','B'),5),
                   First = rnorm(10),
                   Second = rnorm(10),
                   Third = rnorm(10))

我想要的是将整个数据框除以 A 的平均值。我可以通过以下方式计算:

a.mean <- sapply(data[data$Names == 'A', 2:4], mean)

但是当我尝试将整个数据框除以这样的向量时,我没有得到正确的值:

normalized.data <- data[2:4]/a.mean
normalized.data$Names <- data$Names
sapply(data[normalized.data$Names == 'A', 2:4], mean)

First     Second      Third 
0.2578018 -0.5864073  0.1156760

我想要的是 A 的标准化平均值现在等于 1。有没有办法做到这一点?

4

3 回答 3

4
set.seed(99)
data <- data.frame(Names=rep(c('A','B'),5),
                   First = rnorm(10),
                   Second = rnorm(10),
                   Third = rnorm(10))

a.mean <- sapply(data[data$Names == 'A', 2:4], mean)
data[,2:4] <- sweep(data[,2:4],MARGIN=2,a.mean,"/")

(norm.mean <- sapply(data[data$Names == 'A', 2:4], mean))
## First Second  Third 
##     1      1      1 

根据您的应用程序,将Names列改为行名可能更容易:

data <- data.frame(First = rnorm(10),
                   Second = rnorm(10),
                   Third = rnorm(10),
                   row.names=rep(c('A','B'),5))

我也喜欢subset(data,Names=='A')可读性(尽管不推荐用于编程:请参阅https://github.com/hadley/devtools/wiki/Evaluation

于 2013-02-28T14:16:24.513 回答
2
set.seed(99)
data <- data.frame(Names=rep(c('A','B'),5),
                   First = rnorm(10),
                   Second = rnorm(10),
                   Third = rnorm(10))

a.mean <- colMeans(data[data$Names == 'A', 2:4])


normalized.data <- as.data.frame(t(t(data[,2:4])/a.mean))


normalized.data$Names <- data$Names
colMeans(normalized.data[normalized.data$Names == 'A', 1:3])

#First Second  Third 
#1      1      1 
于 2013-02-28T14:17:20.380 回答
2

哦,没关系:你并没有像你认为的那样分裂。将矩阵除以值向量不会将每一列除以给定值。

Rgames> foo
     [,1] [,2] [,3]
[1,]    5    3    7
[2,]    5    3    7
[3,]    5    3    7
[4,]    5    3    7
[5,]    5    3    7
Rgames> foo/c(1,2,3)
         [,1] [,2]     [,3]
[1,] 5.000000  1.0 3.500000
[2,] 2.500000  3.0 2.333333
[3,] 1.666667  1.5 7.000000
[4,] 5.000000  1.0 3.500000
[5,] 2.500000  3.0 2.333333
于 2013-02-28T14:20:58.783 回答