如何改变mutate中的另一个数据框架

时间:2017-10-09 08:54:09

标签: r dataframe dplyr mutate

我有两个数据框:

dane <- data.frame(x = 1:5, y = 5:1, Salary =  c(1,2,3,2,1))
> dane
  x y Salary
1 1 5      1
2 2 4      2
3 3 3      3
4 4 2      2
5 5 1      1

x <- dane %>% select(-Salary) %>% gather() %>% arrange(key, value) %>%
group_by(key) %>% mutate(value = (value + lead(value)) / 2) %>% na.omit()
> x
# A tibble: 8 x 2
# Groups:   key [2]
    key value
  <chr> <dbl>
1     x   1.5
2     x   2.5
3     x   3.5
4     x   4.5
5     y   1.5
6     y   2.5
7     y   3.5
8     y   4.5

现在我想计算一个这样的错误(我希望mutate_ mutate中的另一个数据框使用第一个中的值):

x %>% mutate(error = dane %>%
             mutate_(gr = paste("ifelse(", key, "<=", value, ", 0, 1)")) %>%
             group_by(gr) %>%
             mutate(pred = mean(Salary)) %>%
             summarise(error = sum((Salary-pred)^2)) %>%
             select(error) %>%
             sum())

但结果我得到了这样的结果:

# A tibble: 8 x 3
# Groups:   key [2]
    key value error
  <chr> <dbl> <dbl>
1     x   1.5     2
2     x   2.5     2
3     x   3.5     2
4     x   4.5     2
5     y   1.5     2
6     y   2.5     2
7     y   3.5     2
8     y   4.5     2

我的每一行都使用keyvalue列的相同值。我该怎么做才能解决它?

1 个答案:

答案 0 :(得分:0)

似乎rowwise函数就是答案:

x %>% rowwise() %>% mutate(error = dane %>%
         mutate_(gr = paste("ifelse(", key, "<=", value, ", 0, 1)")) %>%
         group_by(gr) %>%
         mutate(pred = mean(Salary)) %>%
         summarise(error = sum((Salary-pred)^2)) %>%
         select(error) %>%
         sum())

Source: local data frame [8 x 3]
Groups: <by row>

# A tibble: 8 x 3
    key value error
  <chr> <dbl> <dbl>
1     x   1.5   2.0
2     x   2.5   2.5
3     x   3.5   2.5
4     x   4.5   2.0
5     y   1.5   2.0
6     y   2.5   2.5
7     y   3.5   2.5
8     y   4.5   2.0