与mutate_at dplyr相反

时间:2018-04-30 06:38:44

标签: r dplyr

我们知道来自mutate_at的{​​{1}}函数允许我们改变选定的多个列并将函数应用于每个列。我需要与之相反。我的意思是说,将多个函数应用于同一列或多次将同一函数应用于同一列。请使用以下dplyr

reproducible example

我只关心df > main <- structure(list(PolygonId = c(0L, 1L, 1612L, 3L, 2L, 1698L), Area = c(3.018892, 1.995702, 0.582808, 1.176975, 2.277057, 0.014854), Perimeter = c(10.6415, 8.6314, 4.8478, 6.1484, 9.2226, 0.6503), h0 = c(1000,500,700,1000,200,1200)), .Names = c("PolygonId", "Area", "Perimeter", "h0"), row.names = c(NA, 6L), class = "data.frame") > main PolygonId Area Perimeter h0 1 0 3.018892 10.6415 1000 2 1 1.995702 8.6314 500 3 1612 0.582808 4.8478 700 4 3 1.176975 6.1484 1000 5 2 2.277057 9.2226 200 6 1698 0.014854 0.6503 1200 中的h0列。

预期结果:
main字段为h10h0 + 10% of h0h_10

h0 - 10% of h0

我通常会这样做::

  PolygonId     Area Perimeter      h0     h10      h20    h_10   h_20
1         0 3.018892   10.6415    1000    1100     1200    900     800
2         1 1.995702    8.6314     500     550      600    450     400
3      1612 0.582808    4.8478     700     770      840    630     560
4         3 1.176975    6.1484    1000    1100     1200    900     800
5         2 2.277057    9.2226     200     220      240    180     160
6      1698 0.014854    0.6503    1200    1320     1440   1080     960

但是,由于我必须以正面和负面的方式对calcH <- function(h, pc){ h + pc / 100 * h } new_main <- mutate ( main, h10 = calcH(h0, 10), h20 = calcH(h0, 20), h_10 = calcH(h0, -10), h_20 = calcH(h0, -20) ) 进行计算,所以这将是忙乱而长的代码。

4 个答案:

答案 0 :(得分:1)

我喜欢使用长数据表示来解决这些问题:

library(dplyr)
library(tidyr)

# create data frame with join helper and multiplier-values:
bla <- data.frame(mult = seq(-.1, .1, .01), 
                  join = TRUE)

# join, calculate values, create names, transform to wide:
main %>% 
  mutate(join = TRUE) %>% 
  left_join(bla) %>% 
  mutate(h0 = h0*(1+mult),
         mult = sub(x = paste0("h", mult*100), pattern = "-", replacement = "_")) %>% 
  select(-join) %>% 
  spread(mult, h0)

答案 1 :(得分:1)

这在基础R中很容易。想法是创建一个具有所需百分比的向量,在该向量上循环并计算您的度量,即

v1 <- c(1, seq(2.5, 30, by = 2.5), seq(-30, -2.5, by = 2.5), -1)
sapply(v1, function(i) calcH(main$h0, i))

答案 2 :(得分:1)

mutate_at可以使用多个函数,但它们需要在环境中作为命名函数存在(不能是匿名函数)所以像

pcts<-rep(c(1,2.5*1:12),2)*c(-1,1)
for(i in pcts){
    assign(gsub("-","_",paste0("h",i)),eval(parse(text=sprintf("function(x) x*(100+%f)/100",i))))    }

main %>% mutate_at(vars(h0),gsub("-","_",paste0("h",pcts)))

会起作用

答案 3 :(得分:0)

这是另一种类似于@andyyy的方法,但是使用rlang

library(dplyr)
library(rlang)

percent <- c(1, 2.5*1:12)

calc_expr <- function(percent_vec){
  parse_exprs(paste(paste0("h0+(",percent_vec,"/100*h0)"), collapse = ";"))
}

main %>%
  mutate(!!!calc_expr (percent), !!!calc_expr (percent*-1)) %>%
  setNames(c(colnames(main), paste0("h", percent), paste0("h_", percent)))

结果:

  PolygonId     Area Perimeter   h0   h1   h2.5   h5   h7.5  h10  h12.5  h15  h17.5  h20  h22.5  h25  h27.5
1         0 3.018892   10.6415 1000 1010 1025.0 1050 1075.0 1100 1125.0 1150 1175.0 1200 1225.0 1250 1275.0
2         1 1.995702    8.6314  500  505  512.5  525  537.5  550  562.5  575  587.5  600  612.5  625  637.5
3      1612 0.582808    4.8478  700  707  717.5  735  752.5  770  787.5  805  822.5  840  857.5  875  892.5
4         3 1.176975    6.1484 1000 1010 1025.0 1050 1075.0 1100 1125.0 1150 1175.0 1200 1225.0 1250 1275.0
5         2 2.277057    9.2226  200  202  205.0  210  215.0  220  225.0  230  235.0  240  245.0  250  255.0
6      1698 0.014854    0.6503 1200 1212 1230.0 1260 1290.0 1320 1350.0 1380 1410.0 1440 1470.0 1500 1530.0
   h30  h_1  h_2.5  h_5  h_7.5 h_10 h_12.5 h_15 h_17.5 h_20 h_22.5 h_25 h_27.5 h_30
1 1300  990  975.0  950  925.0  900  875.0  850  825.0  800  775.0  750  725.0  700
2  650  495  487.5  475  462.5  450  437.5  425  412.5  400  387.5  375  362.5  350
3  910  693  682.5  665  647.5  630  612.5  595  577.5  560  542.5  525  507.5  490
4 1300  990  975.0  950  925.0  900  875.0  850  825.0  800  775.0  750  725.0  700
5  260  198  195.0  190  185.0  180  175.0  170  165.0  160  155.0  150  145.0  140
6 1560 1188 1170.0 1140 1110.0 1080 1050.0 1020  990.0  960  930.0  900  870.0  840

注释:

使用百分比向量,我使用paste0parse_exprs构造了多个表达式,然后取消引号,并使用mutate将它们拼接在!!!中。最后,使用setNames重命名列。