尝试将计算列添加到表中

时间:2018-02-08 14:47:14

标签: r

我在R中有一张表:

ftable(final_problem ~ Condition + usedVisualCue, data = data)

                        final_problem   0 0.6
Condition usedVisualCue                      
1         0                             3  10
          1                            12 131
2         0                            63  90
          1                             0   0
3         0                            21  46
          1                            17  60
4         0                             0   0
          1                            20 132

我想在此表的右侧添加另一列。附加列中的每个条目应为0.6列中条目的0.6倍除以0和0.6列中条目的总和。这是它应该是什么样子:

                        final_problem   0 0.6  mean
Condition usedVisualCue                       
1         0                             3  10  0.46
          1                            12 131  0.55
2         0                            63  90  0.35
          1                             0   0   0.0
3         0                            21  46  0.41
          1                            17  60  0.47
4         0                             0   0   0.0
          1                            20 132  0.52

新列是每个Condition类别和usedVisualCue的final_problem的平均值。

有没有人知道添加此列的方法?

这张桌子是下一个最好的东西:

aggregate(final_problem ~ Condition + usedVisualCue, data = data, mean)

  Condition usedVisualCue final_problem
1         1             0     0.4615385
2         2             0     0.3529412
3         3             0     0.4119403
4         1             1     0.5496503
5         3             1     0.4675325
6         4             1     0.5210526

对于最小数据集:

smallData <- head(data,4)

dput(smallData)

structure(list(id = c(18L, 21L, 25L, 27L), Condition = c(1L, 
1L, 1L, 1L), choice = c(0L, 0L, 0L, 0L), correct = c(1L, 1L, 
1L, 0L), plus = c(0.06, 0.06, 0.06, 0.06), fee_for_reminder = c(0, 
0, 0, 0), problem1 = c(0.03, 0.03, 0, 0), problem2 = c(0.03, 
0.03, 0.03, 0), problem3 = c(0.03, 0.03, 0.03, 0), problem4 = c(0.03, 
0.03, 0, 0), problem5 = c(0, 0, 0, 0), final_problem = c(0.6, 
0.6, 0.6, 0), gender = c(1L, 0L, 0L, 0L), age = c(28L, 26L, 28L, 
36L), dup = c(0L, 0L, 0L, 0L), Total_Amount_Earned = c(0.6, 0.6, 
0.6, 0), Total_Amount_Earned_if.forced.to.pay.for.cue = c(0.57, 
0.57, 0.57, -0.03), `filter_$` = c(1L, 1L, 1L, 1L), usedVisualCue = c(0, 
0, 0, 0)), .Names = c("id", "Condition", "choice", "correct", 
"plus", "fee_for_reminder", "problem1", "problem2", "problem3", 
"problem4", "problem5", "final_problem", "gender", "age", "dup", 
"Total_Amount_Earned", "Total_Amount_Earned_if.forced.to.pay.for.cue", 
"filter_$", "usedVisualCue"), row.names = c(NA, -4L), class = c("tbl_df", 
"tbl", "data.frame"))


ftable(final_problem ~ Condition + usedVisualCue, data = smallData)

                        final_problem 0 0.6
Condition usedVisualCue                    
1         0                           1   3

应该看起来像

                        final_problem 0 0.6 mean
Condition usedVisualCue                    
1         0                           1   3 0.45

这是aggregate命令的输出:

aggregate(final_problem ~ Condition + usedVisualCue, data = smallData, mean)

  Condition usedVisualCue final_problem
1         1             0          0.45

1 个答案:

答案 0 :(得分:0)

您可以使用spread中的tidyr功能获取正确的列

df <- as.data.frame(ftable(final_problem ~ Condition + usedVisualCue, data = data))
df <- spread(df, final_problem, Freq)
within(df, mean <-  `0.6` * 0.6 / (`0` + `0.6`))

或者是整齐的风格:

library(tidyverse)
data %>%
  group_by(Condition, usedVisualCue) %>%
  count(final_problem) %>%
  spread(final_problem, n) %>%
  mutate(mean = `0.6` * 0.6 / (`0` + `0.6`))