我有以下数据框和向量:
dframe <- as.data.frame(matrix(1:9,3))
vector <- c(2,3,4)
我想将每一列相乘dframe
通过相应的值vector
。这不会做:
> vector * dframe
V1 V2 V3
1 2 8 14
2 6 15 24
3 12 24 36
each row of dframe
乘以相应的值vector
,不是每个column。有没有惯用的解决方案,或者我是否坚持for
cycle?
这是另一个选项,使用sweep
sweep(dframe, 2, vector, "*")
# V1 V2 V3
#1 2 12 28
#2 4 15 32
#3 6 18 36
或者使用col
dframe*vector[col(dframe)]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)