2017-10-09 79 views
0

我有兩個數據幀中的另一個數據幀:如何變異發生變異

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列的值相同。我該怎麼做才能解決它?

回答

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