【发布时间】:2019-01-30 23:24:48
【问题描述】:
我的样本。
data=structure(list(add = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("x",
"y"), class = "factor"), x1 = c(14L, 15L, 36L, 0L, 0L, 0L, 53L,
10L, 39L, 27L, 67L, 25L, 19L, 49L, 53L, 64L, 61L, 12L, 75L, 34L,
88L, 43L, 85L, 93L, 44L, 31L, 37L, 90L, 66L, 39L, 59L, 96L, 41L,
23L, 20L, 26L, 69L, 28L, 35L, 96L, 87L, 82L, 70L, 68L, 26L, 12L,
58L, 18L, 76L, 93L, 3L, 31L), group = structure(c(2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L), .Label = c("female", "male"), class = "factor")), .Names = c("add",
"x1", "group"), class = "data.frame", row.names = c(NA, -52L))
在此数据中,有组变量(性别(男性和女性)我需要获得所有男性的统计平均值和 25 个百分位,这些男性在女性之前。男性在女性之后,我不碰。还有女性我不触碰。
这是从添加列按组x 和y 拆分的分析。
如果男性在女性之后 x1 > 25 个百分位,这是我们在女性之前为男性计算的,那么这个值必须替换为男性在女性之前的平均值“我们不接触的女性类别。
AntoniosK的解决方案非常好
library(tidyverse)
library(data.table)
data %>%
group_by(add) %>% # for each add do the below...
mutate(group2 = rleid(group)) %>%
group_by(add, group, group2) %>%
mutate(MEAN = mean(x1[group=="male" & group2==1]),
Q25 = quantile(x1[group=="male" & group2==1], 0.25)) %>%
group_by(add) %>% # for each add update x1 values....
mutate(x1 = ifelse(group=="male" & group2==3 & x1 > unique(Q25[!is.na(Q25)]), unique(MEAN[!is.na(MEAN)]), x1)) %>%
ungroup() %>%
select(-group2) %>%
data.frame()
但现在我想用 x1 将 0 值替换为 Na。
data$x1[data$x1 == 0] <- NA
之后,当我取消脚本时,我得到 错误
mutate_impl(.data, dots) 中的错误:评估错误:缺失 如果 'na.rm' 为 FALSE,则不允许使用 NaN。
怎么办,该脚本通过了 NA 并且只使用 int 值?
编辑
data=structure(list(add = c(11202L, 11202L, 11202L, 11202L, 11202L,
11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L,
11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L,
11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L,
11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L,
11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L,
11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L), x1 = c(NA,
2L, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, 1L, 1L, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 3L, NA, NA, NA, NA, 1L, 1L, NA, NA,
NA, NA, NA), group = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("female",
"male"), class = "factor")), .Names = c("add", "x1", "group"), class = "data.frame", row.names = c(NA,
-52L))
library(tidyverse)
library(data.table)
data %>%
group_by(add) %>%
mutate(group2 = rleid(group)) %>%
group_by(add, group, group2) %>%
mutate(MEAN = mean(x1[group=="male" & group2==1]),
Q25 = quantile(x1[group=="male" & group2==1], 0.25)) %>%
group_by(add) %>%
mutate(x1 = ifelse(group=="male" & group2==3 & x1 > unique(Q25[!is.na(Q25)]), unique(MEAN[!is.na(MEAN)]), x1),
x1 = ifelse(x1==0, NA, x1)) %>% # new code added
ungroup() %>%
select(-group2) %>%
data.frame()
编辑2
代码结果
add x1 group MEAN Q25
x 14.00000 male 23.72727 5.0
x 15.00000 male 23.72727 5.0
x 36.00000 male 23.72727 5.0
x 0.00000 male 23.72727 5.0
x 0.00000 male 23.72727 5.0
x 0.00000 male 23.72727 5.0
x 53.00000 male 23.72727 5.0
x 10.00000 male 23.72727 5.0
x 39.00000 male 23.72727 5.0
x 27.00000 male 23.72727 5.0
x 67.00000 male 23.72727 5.0
x 25.00000 female NaN NA
x 19.00000 female NaN NA
x 49.00000 female NaN NA
x 53.00000 female NaN NA
x 64.00000 female NaN NA
x 61.00000 female NaN NA
x 12.00000 female NaN NA
x 23.72727 male NaN NA
x 23.72727 male NaN NA
x 23.72727 male NaN NA
x 23.72727 male NaN NA
x 23.72727 male NaN NA
x 23.72727 male NaN NA
x 23.72727 male NaN NA
x 23.72727 male NaN NA
之后
add x1 group
x 94.90 male
女后前4男的总和=94.90
【问题讨论】:
-
您的列名之一是“此分析如何按组拆分?组”。我在
data中看不到任何列ReturnCount。当我运行该代码时,我在任何地方都看不到任何零值。 -
ReturnCount 是 x1。
-
我认为如果您发布一个
x1为零的示例会更容易,这样我们就可以检查您收到该错误的原因。 -
@AntoniosK,我用零编辑了帖子。请检查
-
@AntoniosK,我提供了。
标签: r dplyr data.table