简体   繁体   English

如何使用分组变量计算 R 中的变量?

[英]How can I compute a variable in R using a grouping variable?

I organized my dataset so that it would look like like this:我组织了我的数据集,使其看起来像这样:

Sample Target Concentration样品目标浓度

sample1 mutant 18.36 sample1 突变体 18.36

sample1 wildtype 3563.34样品 1 野生型 3563.34

sample2 mutant 19.33 sample2 突变体 19.33

sample2 wildtype 3650.24样品 2 野生型 3650.24

sample3 mutant 15.81 sample3 突变体 15.81

sample3 wildtype 3920.16样品 3 野生型 3920.16

Sample Mutant/wildtype样本突变体/野生型

sample1 18.36/356334样品1 18.36/356334

sample2 19.33/3650.24样本2 19.33/3650.24

sample3 15.81/3920.16样品3 15.81/3920.16

I want to calculate the mutant to wild type ratio by sample but couldn't find a specific argument in the mutate function of r for this seemingly simple task.我想按样本计算突变型与野生型的比率,但对于这个看似简单的任务,在 r 的突变 function 中找不到特定参数。

One thing you can do is pivot your data wider so all info about each sample is contained in a single row.您可以做的一件事是 pivot 您的数据更宽,因此有关每个样本的所有信息都包含在一行中。 We will create a new column for "mutant" and for "wildtype" and the values in these columns will be the concentrations.我们将为“mutant”和“wildtype”创建一个新列,这些列中的值将是浓度。

First, I created some dummy data to work with.首先,我创建了一些可以使用的虚拟数据。

data <- data.frame(sample = c(1,1,2,2,3,3), 
                  type = c("m", "w", "m", "w", "m", "w"), 
                  concentration = c(1,2,3,4,5,6))

Dummy data:虚拟数据:

  sample type concentration
1      1    m             1
2      1    w             2
3      2    m             3
4      2    w             4
5      3    m             5
6      3    w             6

Here's what you do:这是你要做的:

library(tidyverse)

data %>% 
  pivot_wider(names_from = type, values_from = concentration) %>% 
  mutate(ratio = m/w) -> data

And this is what you get:这就是你得到的:

# A tibble: 3 × 4
  sample     m     w ratio
   <dbl> <dbl> <dbl> <dbl>
1      1     1     2 0.5  
2      2     3     4 0.75 
3      3     5     6 0.833

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何添加新的分组变量 - How can I add a new grouping variable R中按用户ID分组后可以汇总变量组合吗? - Can I summarise variable combinations after grouping by user ID in R? 如何使用 dplyr 计算同一分组变量中每个变量的相关性? - How i can calculate the correlation of each variable within the same grouping variable using dplyr? 如何根据分组变量从R中的数据帧中删除第n行? - How can I delete every n-th row from a dataframe in R, according to grouping variable? 在 R 中,我如何 label 重复值的第一个实例在按另一个变量分组时在列中运行? - In R, how can I label the first instance of a repeated value run within a column while grouping by another variable? 如何使用 R 以有效的方式计算新变量? - How to compute a new variable in an efficient way using R? 如何使用R计算由两个变量定义的新变量? - How to compute a new variable that is defined by two variables using R? 如何使用 r 计算我的 x 分类变量(暴露)的前 84 行 select 来计算我的连续 y 变量的平均值? - How do I select the first 84 rows of my x categorical variable (exposed) to compute the mean of my continuous y variable using r? 使用 R 计算变量的均值 - Compute mean across variable using R 使用一个分组变量在 R 中进行预测 - Forecasting in R using one grouping variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM