Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否有函数将一列中的值除以另一列中的每个(!)值?在R_R - Fatal编程技术网

是否有函数将一列中的值除以另一列中的每个(!)值?在R

是否有函数将一列中的值除以另一列中的每个(!)值?在R,r,R,我有一个数据帧(df),看起来像这样: Code Status Value 1 1 Treated 1.5 2 1 Treated 1.7 3 1 Treated 1.9 4 1 Control 2.1 5 1 Control 2.3 6 1 Control 2.5 7 2 Treated 1.6 8 2 Treated 1.8 9 2 Treated 2.0 10 2 Co

我有一个数据帧(df),看起来像这样:

   Code  Status Value
1     1 Treated   1.5
2     1 Treated   1.7
3     1 Treated   1.9
4     1 Control   2.1
5     1 Control   2.3
6     1 Control   2.5
7     2 Treated   1.6
8     2 Treated   1.8
9     2 Treated   2.0
10    2 Control   2.4
11    2 Control   2.6
12    2 Control   2.8
# 1st "Value" in "Code" 1
df$Value[1]/df$Value[4]
df$Value[1]/df$Value[5]
df$Value[1]/df$Value[6]
# 2nd "Value" in "Code" 1
df$Value[2]/df$Value[4]
df$Value[2]/df$Value[5]
df$Value[2]/df$Value[6]
for(i in 1:2){
  for(j in 1:3){
 x <- df$Value[df$Code == i & df$Status == "Treated"]
 y <- df$Value[df$Code == i & df$Status == "Control"]
 vector <- x/y[j]
 print(vector)
  }
} 
代码如下:

df <- data.frame(Code = rep(c(1:2), each = 6),
           Status = rep(rep(c("Treated", "Control"), each = 3), 2),
           Value = c(1.5, 1.7, 1.9, 2.1, 2.3, 2.5, 1.6, 1.8, 2.0, 2.4, 2.6, 2.8))
等等。。。为此,我创建了一个for循环,如下所示:

   Code  Status Value
1     1 Treated   1.5
2     1 Treated   1.7
3     1 Treated   1.9
4     1 Control   2.1
5     1 Control   2.3
6     1 Control   2.5
7     2 Treated   1.6
8     2 Treated   1.8
9     2 Treated   2.0
10    2 Control   2.4
11    2 Control   2.6
12    2 Control   2.8
# 1st "Value" in "Code" 1
df$Value[1]/df$Value[4]
df$Value[1]/df$Value[5]
df$Value[1]/df$Value[6]
# 2nd "Value" in "Code" 1
df$Value[2]/df$Value[4]
df$Value[2]/df$Value[5]
df$Value[2]/df$Value[6]
for(i in 1:2){
  for(j in 1:3){
 x <- df$Value[df$Code == i & df$Status == "Treated"]
 y <- df$Value[df$Code == i & df$Status == "Control"]
 vector <- x/y[j]
 print(vector)
  }
} 
我的问题是,我需要对这些结果进行进一步的计算。 但是,当我试图提取向量,以便使用它时,它只包含最后一次计算:

[1] 0.5714286 0.6428571 0.7142857
有更好的计算方法吗


上下文提示:稍后我需要为每个“代码”计算MFV(隶属函数值),但我需要使用“值”中所有计算(除以)值的最小值/最大值。让我们来分析这个问题。假设只有一个代码。该问题可通过以下方式解决:

df%>%
过滤器(代码==1)%>%#-->现在只需选择一个代码
分组依据(状态)%>%-->每个状态
mutate(Trial=seq_-along(Value))%>%#-->将助手ID添加到每行
排列(状态、值)%>%#-->将行转换为列
展开(控制,处理)%>%#-->创建所有可能的组合
突变(比率=治疗/对照)#-->计算比率
一旦我们有了这个,我们只需要能够处理每个代码。我们可以通过一个简单的循环来实现,这里我使用的是
lappy
,但是
map
from
purr
或者
for
循环也可以实现同样的效果


进程%
分组依据(状态)%>%
变异(试验=沿(值)顺序排列))%>%
价差(状态、价值)%>%
扩展(控制,处理)%>%
突变(比率=治疗/对照)
}
df%>%
嵌套(-Code)%%>%
变异(数据=lappy(数据,过程))%>%
unnest(数据)

这里是另一种
tidyverse
方法。您可以按每个
code
nest
对数据进行分组

然后,对于每个
code
,您可以使用
outer
给出两个向量的乘积(处理和控制)-但是,您可以提供函数
/
来给出一个比率(除法)

unest
之后,将有一个名为
Ratio
的列,其中将包含一个数字矩阵。请注意,您的结果中仍有
code
列,这将是未来计算所需的

library(dplyr)
library(tidyr)
library(purrr)

df %>%
  group_by(Code) %>%
  nest() %>%
  mutate(Ratio = map(data, ~outer(.x$Value[.x$Status == "Treated"], .x$Value[.x$Status == "Control"], `/`))) %>%
  unnest(Ratio) 
输出

   Code data             Ratio[,1]  [,2]  [,3]
  <int> <list>               <dbl> <dbl> <dbl>
1     1 <tibble [6 × 2]>     0.714 0.652 0.6  
2     1 <tibble [6 × 2]>     0.810 0.739 0.68 
3     1 <tibble [6 × 2]>     0.905 0.826 0.76 
4     2 <tibble [6 × 2]>     0.667 0.615 0.571
5     2 <tibble [6 × 2]>     0.75  0.692 0.643
6     2 <tibble [6 × 2]>     0.833 0.769 0.714
代码数据比率[,1][,2][,3]
1     1      0.714 0.652 0.6  
2     1      0.810 0.739 0.68 
3     1      0.905 0.826 0.76 
4     2      0.667 0.615 0.571
5     2      0.75  0.692 0.643
6     2      0.833 0.769 0.714