R 将不同数据帧行中的每个值除以向量中存储的值
我有两个数据帧df1和df2,它们存储在一个列表中:R 将不同数据帧行中的每个值除以向量中存储的值,r,R,我有两个数据帧df1和df2,它们存储在一个列表中: $df1 column 1. column 2. column 3 A a6 1 B j7 2 C i0 3 D b2 4 $df2 column 1. column 2. column 3 E
$df1
column 1. column 2. column 3
A a6 1
B j7 2
C i0 3
D b2 4
$df2
column 1. column 2. column 3
E 8h 1
F 4d 2
G 3n 3
H a9 4
I 5% 5
L *9. 6
数据帧具有不同的行号,存储在列表1中
列表1我们循环遍历数据集的
列表,使用Map
和transform
将最后一列除以存储在向量中的该数据集的行数('vec')
嗨,Akrun,我试过你的代码,对不起,是的,当我想用数据帧的行数除以第3列时,这是正确的。@Paololorenzin对不起,问题是什么?噢,让我们假设行号不是4或6,而是存储在向量(如10和20)中的其他数字,vecHi Akrun,非常感谢,我使用的是“扫描”但是“地图”更好,现在它开始工作了,我在一个for循环中写了这样的东西:我投票支持你的答案(这很有帮助),但我的声誉不到15,所以它说它不显示,无论如何,非常感谢你的帮助
$df1
$df1 nrow = 4
#df1_ALL nrow = 10
column 1. column 2. column 3
A a6 1/10
B j7 2/10
C i0 3/10
D b2 4/10
$df2
$df2 nrow=6
$df2_ALL nrow = 20
column 1. column 2. column 3
E 8h 1/20
F 4d 2/20
G 3n 3/20
H a9 4/20
I 5% 5/20
L *9. 6/20
list2<-list()
for (i in 1: length(list1)) {
+ list2[[i]]<- sweep(list1[[i]]$column3, 1,vec,'/')
+ }
Error in array(STATS, dims[perm]) : 'dims' cannot be of length 0
In addition: Warning message:
In sweep(myfiles[[i]]$rank, 1, g1, "/") :
STATS is longer than the extent of 'dim(x)[MARGIN]'
Map(function(x, y) transform(x, `column 3` = `column 3`/y), list1, vec)