我正在try 将一个数组列表乘以一个数据帧列表,但结果并不像我想象的那样.

数组是维度为5x2的2的列表,数据帧是维度为5x2的2的列表.以下是数据

mat1 <- list(emote = structure(c(32.159, -3.608, -4.763, 
                         5.394, 8.581, 71.258, 0.427, -0.323, 10.427, 21.967), 
                       dim = c(5L, 2L), 
                       dimnames = list(c("(Icpt)", "pl", "ed", "fin", "hp"), c("A", "B"))), 
     sceng = structure(c(105.992, -0.011, 5.847, -10.575, 
                         -24.148, 229.327, 7.864, 18.654, -3.56, -8.6), 
                       dim = c(5L, 2L), 
                       dimnames = list(c("(Icpt)", "pl", "ed", "fin", "hp"), c("A", "B"))))

df1 <- list(emote = structure(list(Var1 = structure(1:5, levels = c("(Icpt)", "pl", "ed", "fin", "hp"), 
                                   class = "factor"), value = c(0.079, 0.013, 0.005, 0.017, 0.013)), 
                               row.names = c(NA, -5L), class = "data.frame"), 
             sceng = structure(list(Var1 = structure(1:5, levels = c("(Icpt)", "pl", "ed", "fin", "hp"), 
                                   class = "factor"), value = c(0.136, 0.027, 0.009, 0.032, 0.021)), 
                               row.names = c(NA, -5L), class = "data.frame"))

我想做的是将数组emote的列A和B中的值乘以dataframe语句的列value中的值,并将数组sceng中的列A和B的值乘以dataframe sceng的列value中的值.

在理想的情况下,我认为像lapply(mat1, '*', df1)这样的脚本应该可以工作.我还用了 mapply(function(x, y) if(all(is.numeric(x), is.numeric(y))) x * y else x, mat1, df1) 排除字符值,但结果是错误的.最终数据帧/数组 我正在寻找将如下所示的东西:

$emote
        A       B
(Icpt) 2.5406   5.6294
pl    -0.0469   0.0056
ed    -0.0238  -0.0016
fin    0.0917   0.1773
hp     0.1116   0.2856
        
$sceng
        A       B       
(Icpt) 14.4149  31.1885
pl    -0.0003   0.2123
ed     0.0526   0.1679
fin   -0.3384  -0.1139
hp    -0.5071  -0.1806

推荐答案

您只需要将它与df1value列相乘.试着-

res <- Map(function(x, y) x * y$value, mat1, df1)
res

#$emote
#               A         B
#(Icpt)  2.540561  5.629382
#pl     -0.046904  0.005551
#ed     -0.023815 -0.001615
#fin     0.091698  0.177259
#hp      0.111553  0.285571

#$sceng
#               A         B
#(Icpt) 14.414912 31.188472
#pl     -0.000297  0.212328
#ed      0.052623  0.167886
#fin    -0.338400 -0.113920
#hp     -0.507108 -0.180600

R相关问答推荐

棒棒糖图表大小和线宽参数故障标签未出现

在ggplot的注释表格中突出显示最大值

R的GG平行坐标图中的排序变量

更改Heatmap Annotation对象的名称

使用R中的Shapetime裁剪格栅文件

lightgbm发动机在tidymmodels中的L1正则化""

制作等距离的线串副本

使用across,starts_with和ifelse语句变更多个变量

哪一行和行和 Select 特定行,但是考虑到Nas

根据约束随机填充向量的元素

如何使用字符串从重复的模式中提取多个数字?

仅当后续值与特定值匹配时,才在列中回填Nas

R中时间间隔的大向量与参考时间间隔的相交

网络抓取新闻标题和时间

名字的模糊匹配

按镜像列值自定义行顺序

R中的Desolve:返回的导数数错误

条形图中的条形图没有try 赋予它们的 colored颜色

R dplyr::带有名称注入(LHS of:=)的函数,稍后在:=的RHS上引用

图中显示错误 colored颜色 的图例geom_sf