Eu tenho alguns dados que se parecem com o seguinte:
$`2013 Jul`
# A tibble: 3 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.000655 -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
3 0.2 0.2 0 0.2 0 0 0.2 0.2 0 0 0 0
O que estou tentando fazer é quando a linha 2 == 1, multiplica a linha 3 por -1. De tal forma que o resultado esperado seria:
$`2013 Jul`
# A tibble: 3 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.000655 -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
3 -0.2 0.2 0 0.2 0 0 -0.2 -0.2 0 0 0 0
Eu estou tentando fazer isso usando as map
funções e tidyr
.
Dados:
lst1 <- list(`2013 Jul` = structure(list(AAPL = c(-0.00252413896048252,
1, 0.2), AMD = c(0.00385385230384388, 5, 0.2), ADI = c(0.000313658814841043,
2, 0), ABBV = c(0.00148473194650269, 5, 0.2), A = c(0.000231372267065186,
2, 0), APD = c(0.000654593370621786, 3, 0), AA = c(-0.00106999405402468,
1, 0.2), CF = c(-0.00136811540143579, 1, 0.2), NVDA = c(0.000886435916090005,
4, 0), HOG = c(0.000806051331850114, 4, 0), WMT = c(0.000689490484865284,
3, 0), AMZN = c(0.000614708184565435, 3, 0)), row.names = c(NA,
-3L), class = c("tbl_df", "tbl", "data.frame")), `2013 Aug` = structure(list(
AAPL = c(0.0000471064768722691, 1, 0.2), AMD = c(0.00297250845145986,
5, 0.2), ADI = c(0.00110927645875706, 3, 0), ABBV = c(0.00186505842086247,
4, 0), A = c(0.0000542259939665846, 2, 0), APD = c(0.00187188084293685,
5, 0.2), AA = c(-0.000794925865044543, 1, 0.2), CF = c(-0.00109320436559941,
1, 0.2), NVDA = c(0.00139874295083158, 4, 0), HOG = c(0.000699507074667968,
2, 0), WMT = c(0.000964557826996342, 3, 0), AMZN = c(0.00100980845937234,
3, 0)), row.names = c(NA, -3L), class = c("tbl_df", "tbl",
"data.frame")), `2013 Sep` = structure(list(AAPL = c(0.000874550640770086,
3, 0), AMD = c(0.00212896308150426, 5, 0.2), ADI = c(0.000297401899798995,
1, 0.2), ABBV = c(0.00126327568847214, 4, 0), A = c(0.00097767693668047,
3, 0), APD = c(0.00144402630305102, 5, 0.2), AA = c(-0.000734440361937234,
1, 0.2), CF = c(-0.000254998800234454, 1, 0.2), NVDA = c(0.00127259056829905,
4, 0), HOG = c(0.00105093597431519, 3, 0), WMT = c(0.00038339075327491,
2, 0), AMZN = c(0.000479002073488143, 2, 0)), row.names = c(NA,
-3L), class = c("tbl_df", "tbl", "data.frame")), `2013 Oct` = structure(list(
AAPL = c(0.000682565466572836, 2, 0), AMD = c(0.00313699867162714,
5, 0.2), ADI = c(0.000209923665516306, 1, 0.2), ABBV = c(0.000865756791407934,
2, 0), A = c(0.00161631482825611, 4, 0), APD = c(0.00169177940768777,
5, 0.2), AA = c(-0.000319519044240903, 1, 0.2), CF = c(0.00096163857613333,
3, 0), NVDA = c(0.00158604241362254, 4, 0), HOG = c(0.00151424115101764,
3, 0), WMT = c(0.00000265229900199134, 1, 0.2), AMZN = c(0.00124777917896926,
3, 0)), row.names = c(NA, -3L), class = c("tbl_df", "tbl",
"data.frame")), `2013 Nov` = structure(list(AAPL = c(0.00138847413611967,
4, 0), AMD = c(0.00131189086851618, 3, 0), ADI = c(0.000998905149605624,
2, 0), ABBV = c(0.00053428429850944, 1, 0.2), A = c(0.0016278252466143,
4, 0), APD = c(0.00186840190432607, 5, 0.2), AA = c(0.000727945791304539,
1, 0.2), CF = c(0.00128641077503917, 3, 0), NVDA = c(0.000839077672381489,
2, 0), HOG = c(0.00128443125529569, 3, 0), WMT = c(-0.00000406995915300601,
1, 0.2), AMZN = c(0.00279573900270221, 5, 0.2)), row.names = c(NA,
-3L), class = c("tbl_df", "tbl", "data.frame")), `2013 Dec` = structure(list(
AAPL = c(0.00176889092052374, 5, 0.2), AMD = c(-0.000742603775364103,
1, 0.2), ADI = c(0.00044132637464973, 1, 0.2), ABBV = c(0.00113925715965696,
3, 0), A = c(0.00135042334177499, 4, 0), APD = c(0.0012375761024876,
3, 0), AA = c(0.00102055404174894, 2, 0), CF = c(0.00128611035428346,
3, 0), NVDA = c(0.000674203833347995, 2, 0), HOG = c(0.00164877495332821,
4, 0), WMT = c(0.000671450466059644, 1, 0.2), AMZN = c(0.00299158521138261,
5, 0.2)), row.names = c(NA, -3L), class = c("tbl_df", "tbl",
"data.frame")))
1, 2, 3, 4, 5
então quando o valor nessa linha for1
, desejo modificar a linha 3 multiplicando-a por-1
.Respostas:
Você pode fazer:
fonte
Se você não está restrito a usar esses pacotes específicos, uma solução R básica pode ser usar
que faz um loop em todos os quadros de dados de em lst1, subsequentemente em um loop em todas as colunas em cada quadro de dados e altera a terceira linha de acordo com o valor da segunda linha.
fonte
Uma opção com
case_when
emap
fonte
Você pode usar
[<-.data.frame
dentromap
para substituir os elementos na linha 3 em que a linha 2 é 1 pelo negativo.fonte