【问题标题】:dplyr mutate does not work with column names "TRUE" and "FALSE"dplyr mutate 不适用于列名“TRUE”和“FALSE”
【发布时间】:2016-03-14 03:20:02
【问题描述】:

当我使用reshape2时, 我能够获得一个包含名为TRUEFALSE 的列的数据框。 当我尝试使用dplyr 时出现问题 计算TRUE 值的比例, 因为TRUE指的是逻辑值变成1, 而不是名为TRUE 的列。

解决这个问题的自然方法是什么?

require(reshape2)
require(plyr)
require(dplyr)

transplants <- data.frame(donor_region = c(1, 1, 1, 2, 2, 2),
    recipient_region = c(1, 1, 2, 1, 2, 2)) %>%
    mutate(is_self = donor_region == recipient_region)

x <- ddply(transplants, .(donor_region, is_self), summarise,
    freq = length(is_self))
x %>% print

# Compute the proportion of transplants with is_self == TRUE
y <- dcast(x, donor_region ~ is_self, value.var = 'freq') %>%
    mutate(true_proportion = TRUE / (FALSE + TRUE))
y %>% print

# What I get:
#   donor_region FALSE TRUE true_proportion
# 1            1     1    2               1
# 2            2     1    2               1

# What I want to get:
#   donor_region FALSE TRUE true_proportion
# 1            1     1    2       0.6666667
# 2            2     1    2       0.6666667

【问题讨论】:

  • 你可以使用像`TRUE`这样的反引号吗?
  • @thelatemail 您的建议有效。你能建议它作为我可以接受的答案吗?
  • @jenesaisquoi 虽然您的评论没有直接回答问题,但它用更少的代码行完成了我想做的事情。你能把这个写成答案吗?

标签: r dplyr reshape2


【解决方案1】:

我已将 @thelatemail 和 @jenesaisquoi 在 cmets 中给出的答案合并在一起,因为评论部分不是保存答案的最佳位置。

  1. 使用反引号(@thelatemail):

    dcast(x, donor_region ~ is_self, value.var = 'freq') %>%
      mutate(true_proportion = `TRUE` / (`FALSE` + `TRUE`))
    
  2. 使用加权平均值 (@jenesaisquoi):

    x %>% group_by(donor_region) %>% summarise(tp = weighted.mean(is_self, freq))
    

【讨论】:

  • 如果你结合了答案,那么这应该是一份简历。
猜你喜欢
  • 2017-05-22
  • 1970-01-01
  • 2011-11-16
  • 2016-06-10
  • 2016-05-22
  • 1970-01-01
  • 2022-06-13
  • 2017-08-08
  • 2019-06-19
相关资源
最近更新 更多