将数据帧的各种子集乘以不同的向量 [英] Multiply various subsets of a data frame by different vectors

查看:46
本文介绍了将数据帧的各种子集乘以不同的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据框中的几列乘以一个值向量.值的特定向量根据另一列中的值而变化.

I would like to multiply several columns in my data frame by a vector of values. The specific vector of values changes depending on the value in another column.

--编辑--

如果我让数据集更复杂,即超过 2 个条件并且条件在数据集周围随机打乱怎么办?

What if I make the data set more complicated, i.e., more than 2 conditions and the conditions are randomly shuffled around the data set?

这是我的数据集示例:

df=data.frame(
  Treatment=(rep(LETTERS[1:4],each=2)),
  Species=rep(1:4,each=2),
  Value1=c(0,0,1,3,4,2,0,0),
  Value2=c(0,0,3,4,2,1,4,5),
  Value3=c(0,2,4,5,2,1,4,5),
  Condition=c("A","B","A","C","B","A","B","C")
  )

看起来像:

 Treatment Species Value1 Value2 Value3 Condition
     A       1      0      0      0         A
     A       1      0      0      2         B 
     B       2      1      3      4         A
     B       2      3      4      5         C
     C       3      4      2      2         B
     C       3      2      1      1         A
     D       4      0      4      4         B
     D       4      0      5      5         C

如果 Condition=="A",我想将第 3-5 列乘以向量 c(1,2,3).如果 Condition=="B",我想将第 3-5 列乘以向量 c(4,5,6).如果 Condition=="C",我想将第 3-5 列乘以向量 c(0,1,0).因此,生成的数据框将如下所示:

If Condition=="A", I would like to multiply columns 3-5 by the vector c(1,2,3). If Condition=="B", I would like to multiply columns 3-5 by the vector c(4,5,6). If Condition=="C", I would like to multiply columns 3-5 by the vector c(0,1,0). The resulting data frame would therefore look like this:

 Treatment Species Value1 Value2 Value3 Condition
     A       1      0      0      0         A
     A       1      0      0     12         B 
     B       2      1      6     12         A
     B       2      0      4      0         C
     C       3     16     10     12         B
     C       3      2      2      3         A
     D       4      0     20     24         B
     D       4      0      5      0         C

我尝试对数据框进行子集化并乘以向量:

I have tried subsetting the data frame and multiplying by the vector:

t(t(subset(df[,3:5],df[,6]=="A")) * c(1,2,3))

但是我无法将子集化的数据帧返回到原始数据.有没有办法在不设置数据框子集的情况下执行此操作,以便保留其他列(例如,处理、物种)?

But I can't return the subsetted data frame to the original. Is there any way to perform this operation without subsetting the data frame, so that other columns (e.g., Treatment, Species) are preserved?

推荐答案

这是一个相当通用的解决方案,您应该能够根据自己的需要进行调整.

Here's a fairly general solution that you should be able to adapt to fit your needs.

注意outer调用中的第一个参数是逻辑向量,第二个参数是数字,所以在乘法之前转换TRUEFALSE分别为 10.我们可以添加 outer 结果,因为条件不重叠并且 FALSE 元素将为零.

Note the first argument in the outer call is a logical vector and the second is numeric, so before multiplication TRUE and FALSE are converted to 1 and 0, respectively. We can add the outer results because the conditions are non-overlapping and the FALSE elements will be zero.

multiples <-
  outer(df$Condition=="A",c(1,2,3)) +
  outer(df$Condition=="B",c(4,5,6)) +
  outer(df$Condition=="C",c(0,1,0))

df[,3:5] <- df[,3:5] * multiples

这篇关于将数据帧的各种子集乘以不同的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆