Tengo algunos datos que se parecen a los siguientes:
$`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
Lo que intento hacer es cuando la fila 2 == 1, multiplique la fila 3 por -1. Tal que el resultado esperado sería:
$`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
Estoy tratando de hacer esto usando las map
funciones y tidyr
.
Datos:
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
así que cuando el valor en esta fila es1
entonces quiero modificar la fila 3 multiplicándola por-1
.Respuestas:
Tu puedes hacer:
fuente
Si no está restringido a usar esos paquetes específicos, una solución base R podría ser usar
que recorre todos los marcos de datos de en lst1, posteriormente recorre todas las columnas en cada marco de datos y cambia la tercera fila de acuerdo con el valor de la segunda fila.
fuente
Una opción con
case_when
ymap
fuente
Puede usar
[<-.data.frame
dentromap
para reemplazar los elementos en la fila 3 donde la fila 2 es 1 con su negativo.fuente