mutate_all在r中的列表上使用map和case_when / ifelse

时间:2019-11-27 22:22:01

标签: r

我的数据看起来类似于以下内容:

const input = [
  [1, 2, 3, 4],
  [12, 13, 14, 5],
  [11, 16, 15, 6],
  [10, 9, 8, 7]
];

function snail(array) {
  var res = [];
  
  if (!array.length) return res;
  var next = array.shift();
  if (next) res = res.concat(next);
  for (var i = 0; i < array.length; i++) {
    res.push(array[i].pop());
  }
  next = array.pop()
  if (next) res = res.concat(next.reverse());
  for (var i = array.length - 1; i >= 0; i--) {
    res.push(array[i].shift());
  }

  return res.concat(snail(array));
}
console.log(snail(input));

我试图在$`2013 Jul` # A tibble: 2 x 12 AAPL AMD ADI ABBV A APD AA CF NVDA HOG WMT AMZN <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> 1 -0.00252 0.00385 0.000314 0.00148 0.000231 0.000644 -0.00107 -0.00137 0.000886 0.000806 0.000689 0.000615 2 1 5 2 5 2 3 1 1 4 4 3 3 $`2013 Aug` # A tibble: 2 x 12 AAPL AMD ADI ABBV A APD AA CF NVDA HOG WMT AMZN <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> 1 0.0000471 0.00297 0.00111 0.00187 0.0000542 0.00186 -0.000795 -0.00109 0.00140 0.000700 0.000965 0.00101 2 1 5 3 5 2 4 1 1 4 2 3 3 列表中的case_whendplyrmutate函数中使用map。在这里,我将所有包含5或1的列表替换为0.5,将所有其他列表都替换为0。

我正在尝试tibble的变体,但不知道如何将mutate_allcase_when一起使用。我也愿意就如何解决这个问题提出其他建议。

mutate_all

数据

x[1] %>% 
  mutate_all(case_when(... = 5 ~ 0.5))

编辑:

是否可以确保数字总和为1?例如,当前我们有:

list(`2013 Jul` = structure(list(AAPL = c(-0.00252413896048252, 
1), AMD = c(0.00385385230384388, 5), ADI = c(0.000313658814841043, 
2), ABBV = c(0.00148473194650269, 5), A = c(0.000231372267065186, 
2), APD = c(0.000644411609229898, 3), AA = c(-0.00106999405402468, 
1), CF = c(-0.00136811540143579, 1), NVDA = c(0.000886436095375894, 
4), HOG = c(0.000806051331850114, 4), WMT = c(0.000689490484865284, 
3), AMZN = c(0.000614708184565435, 3)), row.names = c(NA, -2L
), class = c("tbl_df", "tbl", "data.frame")), `2013 Aug` = structure(list(
    AAPL = c(0.0000471064768722691, 1), AMD = c(0.00297250845145986, 
    5), ADI = c(0.00110927645875706, 3), ABBV = c(0.00186505842086247, 
    5), A = c(0.0000542259939665846, 2), APD = c(0.00186178155179209, 
    4), AA = c(-0.000794925865044543, 1), CF = c(-0.00109320436559941, 
    1), NVDA = c(0.00139874293303574, 4), HOG = c(0.000699507074667968, 
    2), WMT = c(0.000964557826996342, 3), AMZN = c(0.00100980845937234, 
    3)), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"
)), `2013 Sep` = structure(list(AAPL = c(0.000874550640770086, 
3), AMD = c(0.00212896308150426, 5), ADI = c(0.000297401899798995, 
1), ABBV = c(0.00126327568847214, 4), A = c(0.00097767693668047, 
3), APD = c(0.00143416399096666, 5), AA = c(-0.000734440361937234, 
1), CF = c(-0.000254998800234454, 1), NVDA = c(0.00127259109916125, 
4), HOG = c(0.00105093597431519, 3), WMT = c(0.00038339075327491, 
2), AMZN = c(0.000479002073488143, 2)), row.names = c(NA, -2L
), class = c("tbl_df", "tbl", "data.frame")), `2013 Oct` = structure(list(
    AAPL = c(0.000682565466572836, 2), AMD = c(0.00313699867162714, 
    5), ADI = c(0.000209923665516306, 1), ABBV = c(0.000865756791407934, 
    2), A = c(0.00161631482825611, 4), APD = c(0.00168294315897343, 
    5), AA = c(-0.000319519044240903, 1), CF = c(0.00096163857613333, 
    3), NVDA = c(0.00158604314072248, 4), HOG = c(0.00151424115101764, 
    3), WMT = c(0.00000265229900199134, 1), AMZN = c(0.00124777917896926, 
    3)), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"
)), `2013 Nov` = structure(list(AAPL = c(0.00138847413611967, 
4), AMD = c(0.00131189086851618, 3), ADI = c(0.000998905149605624, 
2), ABBV = c(0.00053428429850944, 1), A = c(0.0016278252466143, 
4), APD = c(0.0018596391559994, 5), AA = c(0.000727945791304539, 
1), CF = c(0.00128641077503917, 3), NVDA = c(0.000839077531026809, 
2), HOG = c(0.00128443125529569, 3), WMT = c(-0.00000406995915300601, 
1), AMZN = c(0.00279573900270221, 5)), row.names = c(NA, -2L), class = c("tbl_df", 
"tbl", "data.frame")), `2013 Dec` = structure(list(AAPL = c(0.00176889092052374, 
5), AMD = c(-0.000742603775364103, 1), ADI = c(0.00044132637464973, 
1), ABBV = c(0.00113925715965696, 3), A = c(0.00135042334177499, 
4), APD = c(0.00122867453428789, 3), AA = c(0.00102055404174894, 
2), CF = c(0.00128611035428346, 3), NVDA = c(0.000674203471055744, 
2), HOG = c(0.00164877495332821, 4), WMT = c(0.000671450466059644, 
1), AMZN = c(0.00299158521138261, 5)), row.names = c(NA, -2L), class = c("tbl_df", 
"tbl", "data.frame")))

这是正确的。但是,我现在看到有四个$`2017 Aug` # A tibble: 2 x 12 AAPL AMD ADI ABBV A APD AA CF NVDA HOG WMT AMZN <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> 1 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0.5 0 0 0 0 0.5 0.5 0.5 0.5 0 。是否可以将输出获取为:

0-5

1 个答案:

答案 0 :(得分:1)

因为它是list,所以我们可以遍历list

map(lst1, ~ .x %>%
                mutate_all(~ case_when(. %in% c(5, 1) ~ .5, 
                                        TRUE ~ 0)) )

在这种情况下,我们也可以在没有case_when的情况下做到这一点

map(lst1, ~ .x %>%
               mutate_all( ~ c(0, .5)[(. %in% c(5, 1)) + 1]))

如果我们需要将列的总和设为1,对于没有sum等于1(因为只有两行)的情况,一种选择是从1中减去第一个数字

map(lst1, ~ .x %>%
            mutate_all(~ case_when(. %in% c(5, 1) ~ .5, 
                                    TRUE ~ 0) ) %>% 
            mutate_all(~ if(sum(.) != 1) replace(., 1, 1 - sum(.)) else .)  )

或者如果我们需要规范化行

map(lst1, ~ .x %>%
            mutate_all(~ case_when(. %in% c(5, 1) ~ .5, 
                                    TRUE ~ 0) ) %>%
         pmap_dfr(., ~ 
                  {v1 <- c(...)
                   v2 <- if(sum(v1) > 1) replace(v1, v1 != 0, 1/sum(v1!=0)) else v1
                   as.list(v2)}))
#$`2013 Jul`
# A tibble: 2 x 12
#   AAPL   AMD   ADI  ABBV     A   APD    AA    CF  NVDA   HOG   WMT  AMZN
#  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1   0     0       0   0       0     0   0     0       0     0     0     0
#2   0.2   0.2     0   0.2     0     0   0.2   0.2     0     0     0     0

#$`2013 Aug`
# A tibble: 2 x 12
#   AAPL   AMD   ADI  ABBV     A   APD    AA    CF  NVDA   HOG   WMT  AMZN
#  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1   0     0       0   0       0     0   0     0       0     0     0     0
#2   0.2   0.2     0   0.2     0     0   0.2   0.2     0     0     0     0

#$`2013 Sep`
# A tibble: 2 x 12
#   AAPL   AMD   ADI  ABBV     A   APD    AA    CF  NVDA   HOG   WMT  AMZN
#  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1     0   0     0       0     0   0     0     0       0     0     0     0
#2     0   0.2   0.2     0     0   0.2   0.2   0.2     0     0     0     0

#$`2013 Oct`
# A tibble: 2 x 12
#   AAPL   AMD   ADI  ABBV     A   APD    AA    CF  NVDA   HOG   WMT  AMZN
#  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1     0   0     0       0     0   0     0       0     0     0   0       0
#2     0   0.2   0.2     0     0   0.2   0.2     0     0     0   0.2     0

$`2013 Nov`
# A tibble: 2 x 12
   AAPL   AMD   ADI  ABBV     A   APD    AA    CF  NVDA   HOG   WMT  AMZN
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1     0     0     0   0       0   0     0       0     0     0   0     0  
2     0     0     0   0.2     0   0.2   0.2     0     0     0   0.2   0.2

$`2013 Dec`
# A tibble: 2 x 12
   AAPL   AMD   ADI  ABBV     A   APD    AA    CF  NVDA   HOG   WMT  AMZN
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1   0     0     0       0     0     0     0     0     0     0   0     0  
2   0.2   0.2   0.2     0     0     0     0     0     0     0   0.2   0.2