【发布时间】:2016-03-14 03:20:02
【问题描述】:
当我使用reshape2时,
我能够获得一个包含名为TRUE 和FALSE 的列的数据框。
当我尝试使用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 虽然您的评论没有直接回答问题,但它用更少的代码行完成了我想做的事情。你能把这个写成答案吗?