BI正在尝试将矩阵dataa
的每一列除以向量mu_i
中的一个元素。
> dput(dataa)
structure(list(`175` = c(2.04, 1.91, 2, 1.92, 1.85, 1.96, 1.88,
1.9), `194` = c(1.66, 1.71, 1.42, 1.76, 1.66, 1.61, 1.55, 1.66
), `213` = c(1.53, 1.54, 1.38, 1.31, 1.35, 1.27, 1.26, 1.38),
`231` = c(1.15, 1.22, 1.17, 1.16, 1.21, 1.28, 1.17, 0), `250` = c(1.26,
0.83, 1.08, 1.02, 1.09, 1.06, 0, 0)), .Names = c("175", "194",
"213", "231", "250"), row.names = c(NA, -8L), class = "data.frame")
> dput(mu_i)
c(1.9325, 1.62875, 1.3775, 1.19428571428571, 1.05666666666667
)
基本上,我想要的是dataa[,i] / mu_i[i]
并从1到5迭代i
。(即{i} ^ $ $ dataa
列除以$ i ^ th $元素mu_i
)我知道我可以执行for循环,但我想知道如何使用apply
函数完成此操作?
更新:
我很感激你的所有答案。我只想问一个问题:
我试图使用像这样的mapply函数来做到这一点
mapply(function(d,m,i)d[,i] - m[i],dataa,mu_i,seq_along(mu_i) )
。我不明白为什么会失败。出于某些原因,我需要将dataa
和mu_i
作为参数传递给apply函数中的函数。我怎么能这样做?
答案 0 :(得分:5)
这是sweep
的用途
sweep(dataa, 2, mu_i, "/")
答案 1 :(得分:3)
您可以在不使用apply
系列函数的情况下执行此操作。尝试transpose
dataa
,然后除以mu_i
t(t(dataa)/mu_i) #returns a matrix output
# 175 194 213 231 250
#[1,] 1.0556274 1.0191865 1.1107078 0.9629187 1.1924290
#[2,] 0.9883571 1.0498849 1.1179673 1.0215311 0.7854890
#[3,] 1.0349288 0.8718342 1.0018149 0.9796651 1.0220820
#[4,] 0.9935317 1.0805833 0.9509982 0.9712919 0.9652997
#[5,] 0.9573092 1.0191865 0.9800363 1.0131579 1.0315457
#[6,] 1.0142303 0.9884881 0.9219601 1.0717703 1.0031546
#[7,] 0.9728331 0.9516500 0.9147005 0.9796651 0.0000000
#[8,] 0.9831824 1.0191865 1.0018149 0.0000000 0.0000000
或者
dataa/mu_i[col(dataa)] #returns a data.frame output
# 175 194 213 231 250
#1 1.0556274 1.0191865 1.1107078 0.9629187 1.1924290
#2 0.9883571 1.0498849 1.1179673 1.0215311 0.7854890
#3 1.0349288 0.8718342 1.0018149 0.9796651 1.0220820
#4 0.9935317 1.0805833 0.9509982 0.9712919 0.9652997
#5 0.9573092 1.0191865 0.9800363 1.0131579 1.0315457
#6 1.0142303 0.9884881 0.9219601 1.0717703 1.0031546
#7 0.9728331 0.9516500 0.9147005 0.9796651 0.0000000
#8 0.9831824 1.0191865 1.0018149 0.0000000 0.0000000
或者如果您需要基于apply
的解决方案
mapply("/", dataa, mu_i) #returns a matrix
# 175 194 213 231 250
#[1,] 1.0556274 1.0191865 1.1107078 0.9629187 1.1924290
#[2,] 0.9883571 1.0498849 1.1179673 1.0215311 0.7854890
#[3,] 1.0349288 0.8718342 1.0018149 0.9796651 1.0220820
#[4,] 0.9935317 1.0805833 0.9509982 0.9712919 0.9652997
#[5,] 0.9573092 1.0191865 0.9800363 1.0131579 1.0315457
#[6,] 1.0142303 0.9884881 0.9219601 1.0717703 1.0031546
#[7,] 0.9728331 0.9516500 0.9147005 0.9796651 0.0000000
#[8,] 0.9831824 1.0191865 1.0018149 0.0000000 0.0000000
如果您需要基于data.frame
解决方案的apply
输出
databa <- dataa
databa[] <- Map("/", dataa, mu_i) #returns `data.frame`
答案 2 :(得分:2)
您可以使用sapply
sapply(1:5, function(i) dataa[,i]/mu_i[i])
[,1] [,2] [,3] [,4] [,5]
[1,] 1.0556274 1.0191865 1.1107078 0.9629187 1.1924290
[2,] 0.9883571 1.0498849 1.1179673 1.0215311 0.7854890
[3,] 1.0349288 0.8718342 1.0018149 0.9796651 1.0220820
[4,] 0.9935317 1.0805833 0.9509982 0.9712919 0.9652997
[5,] 0.9573092 1.0191865 0.9800363 1.0131579 1.0315457
[6,] 1.0142303 0.9884881 0.9219601 1.0717703 1.0031546
[7,] 0.9728331 0.9516500 0.9147005 0.9796651 0.0000000
[8,] 0.9831824 1.0191865 1.0018149 0.0000000 0.0000000