mapping over lists and selecting columns (to create new lists) - r

I am trying to map over some data and select some columns. I try to run the folling:
map(dat$splits, ~ analysis(.x) %>%
as_tibble(., .name_repair = "universal") %>%
map(., ~select(X1, X2, X3, X4, X5, X6, X7, X8, X9)))
But get given an error, however I can see the tibbles by using:
map(dat$splits, ~ analysis(.x))
Which gives:
[[1]]
# A tibble: 20 x 17
date ID var1 var2 Y Y_plus_1 X1 X2 X3 X4 X5 X6 X7 X8
<date> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2016-10-21 CAT1 799. 3.01e-3 1 0 4.14 18 0 1 0.0770 4.29e-4 0.652 -0.976
2 2016-10-24 CAT1 813. 1.72e-2 1 1 4.14 17 0 1 0.0793 4.24e-4 0.634 -0.861
3 2016-10-25 CAT1 808. -6.69e-3 0 1 4.16 16 0 1 0.0804 4.04e-4 0.780 -0.478
4 2016-10-26 CAT1 799. -1.06e-2 0 0 4.16 15 0 1 0.0626 4.26e-4 0.378 -0.332
5 2016-10-27 CAT1 795. -4.66e-3 0 0 4.16 14 0 1 0.0587 4.42e-4 -0.253 -0.647
6 2016-10-28 CAT1 795. 2.52e-5 0 0 4.11 13 0 1 0.0610 4.40e-4 -0.523 -0.753
7 2016-10-31 CAT1 785. -1.36e-2 0 0 4.14 12 0 1 0.0704 4.25e-4 -0.125 -0.620
8 2016-11-01 CAT1 784. -1.19e-3 1 0 3.98 11 0 1 0.0867 4.17e-4 -0.933 -1.30
9 2016-11-02 CAT1 769. -1.90e-2 0 1 6.00 10 0 1 0.0673 4.53e-4 -0.958 -0.793
10 2016-11-03 CAT1 762. -8.55e-3 0 0 8.42 10 0 1 0.0877 4.29e-4 -1.51 -1.17
11 2016-11-04 CAT1 762. -1.44e-4 1 0 7.95 10 0 1 0.0924 4.26e-4 -1.67 -1.21
12 2016-11-07 CAT1 783. 2.69e-2 1 1 7.89 10 0 1 0.0821 4.24e-4 -1.64 -1.06
13 2016-11-08 CAT1 791. 1.02e-2 1 1 7.64 10 0 1 0.0361 4.51e-4 -0.963 0.0116
14 2016-11-09 CAT1 785. -6.58e-3 1 1 4.51 10 0 1 0.0634 4.87e-4 -0.762 1.15
15 2016-11-10 CAT1 763. -2.90e-2 0 1 4.53 10 0 1 0.0290 5.26e-4 -1.32 0.560
16 2016-11-11 CAT1 754. -1.12e-2 0 0 4.40 10 0 1 0.0404 4.94e-4 -1.74 0.142
17 2016-11-14 CAT1 736. -2.38e-2 0 0 4.19 10 0 1 0.0587 4.93e-4 -2.32 -0.504
18 2016-11-15 CAT1 758. 3.04e-2 1 0 4.27 10 0 1 0.125 4.17e-4 -2.74 -1.19
19 2016-11-16 CAT1 764. 7.90e-3 1 1 2.31 29 0 1 0.0637 3.30e-4 -2.16 0.593
20 2016-11-17 CAT1 771. 8.83e-3 1 1 2.31 28
That is I want to map over and select the X1... X8 columns.
Data:
dat <- structure(list(splits = list(structure(list(data = structure(list(
date = structure(c(17095, 17098, 17099, 17100, 17101, 17102,
17105, 17106, 17107, 17108, 17109, 17112, 17113, 17114, 17115,
17116, 17119, 17120, 17121, 17122, 17123, 17126, 17127, 17128,
17130, 17133, 17134, 17135, 17136, 17137, 17140, 17141, 17142,
17143, 17144, 17147, 17148, 17149, 17150, 17151, 17154, 17155,
17156, 17157, 17158, 17162, 17163, 17164, 17165), class = "Date"),
ID = c("CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1"), var1 = c(799.369995, 813.109985, 807.669983,
799.070007, 795.349976, 795.369995, 784.539978, 783.609985,
768.700012, 762.130005, 762.02002, 782.52002, 790.51001,
785.309998, 762.559998, 754.02002, 736.080017, 758.48999,
764.47998, 771.22998, 760.539978, 769.200012, 768.27002,
760.98999, 761.679993, 768.23999, 770.840027, 758.039978,
747.919983, 750.5, 762.52002, 759.109985, 771.190002, 776.419983,
789.289978, 789.27002, 796.099976, 797.070007, 797.849976,
790.799988, 794.200012, 796.419983, 794.559998, 791.26001,
789.909973, 791.549988, 785.049988, 782.789978, 771.820007
), var2 = c(0.00301143592272179, 0.0171885235697395, -0.00669036428079295,
-0.0106478836418512, -0.00465545067066953, 0.0000251700516804565,
-0.0136163258207899, -0.00118539912060411, -0.0190272881732103,
-0.00854690633203736, -0.000144312649125955, 0.0269021803390415,
0.0102105886057713, -0.00657804700031572, -0.0289694516279417,
-0.0111990899370517, -0.0237924756958046, 0.0304450229355975,
0.00789725649510542, 0.0088295314155904, -0.0138609782778413,
0.0113866913646978, -0.0012090379426567, -0.00947587412040363,
0.00090671757719174, 0.00861253683999563, 0.00338440726054889,
-0.016605324777718, -0.0133502127773003, 0.00344958960669994,
0.0160160159893405, -0.00447205963195563, 0.0159133949476373,
0.00678170228664343, 0.0165760738798502, -0.0000252860172512692,
0.00865350998635406, 0.00121847887105075, 0.000978545163097477,
-0.00883623264030775, 0.00429947401567232, 0.00279522911918573,
-0.00233543235943645, -0.00415322695366804, -0.00170618631415476,
0.00207620495506755, -0.00821173659091756, -0.00287881031086645,
-0.0140139389980795), Y = c(1, 1, 0, 0, 0, 0, 0, 1, 0, 0,
1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0,
1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0,
0), Y_plus_1 = c(0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1,
1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0,
1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0), X1 = c(4.13858526513854,
4.13858526513855, 4.16341131085939, 4.16341131085937, 4.16341131085937,
4.11423119297315, 4.13858526513857, 3.97599968560627, 5.99758130881283,
8.41953801047614, 7.95231443679086, 7.88558780320248, 7.6408950559188,
4.51370117323327, 4.52868963859669, 4.39998987943623, 4.18852747359839,
4.27042958796773, 2.30720560360487, 2.3083029424251, 2.3083029424251,
2.30720560360488, 2.30720560360486, 2.33467572807035, 2.33467572807036,
2.33467572807036, 2.30720560360486, 2.33467572807035, 2.31545097851707,
2.3399413414153, 2.40685890963718, 2.40309440701756, 2.33633188340289,
2.3363318834029, 2.23996107961566, 2.23996107961567, 2.23996107961566,
3.14644299189703, 3.1910343925295, 3.4393907031427, 3.30413087760388,
3.33080017630688, 2.63827508869038, 2.99443088216722, 2.99443088216723,
2.99443088216722, 2.99443088216722, 3.14542139469794, 3.14542139469794
), X2 = c(18, 17, 16, 15, 14, 13, 12, 11, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 29, 28, 27, 26, 25, 24, 23, 22, 21,
20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 10, 10, 10, 10,
87, 86, 85, 84, 83, 82, 81), X3 = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0), X4 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), X5 = c(0.0769944316144198,
0.0793311568823971, 0.0803743155230278, 0.0625789438826206,
0.0586506192715035, 0.0610303101083243, 0.0703939970975855,
0.0867098273608016, 0.0673120522212106, 0.0877296725069155,
0.0923742675361241, 0.0821050363880187, 0.0360531976099817,
0.063410467337928, 0.0289807505667197, 0.0403890038946993,
0.0587200889534704, 0.124855015077667, 0.0636602113103218,
0.0748772236055617, 0.0828248414842617, 0.0588561607897347,
0.0437146614571738, 0.0399432627968126, 0.0535895503558405,
0.0538598239712004, 0.0600971764378981, 0.0543532803438423,
0.0412119504402689, 0.0445593481900316, 0.0471324573693227,
0.0366910541674913, 0.0412784111781792, 0.0338162772274317,
0.0391189676384125, 0.051151724195942, 0.0442197922283997,
0.0458769828703159, 0.0392536462039503, 0.0397989336000519,
0.0293505218180493, 0.0229058449521028, 0.0154019371887762,
0.0102366640366435, 0.00783792657548366, 0.014037818210456,
0.00900392496961011, 0.0148108452415051, 0.0159871581537364
), X6 = c(0.000428660536007568, 0.000424348382531349, 0.000403672086504106,
0.000425772306880377, 0.000441567036819891, 0.000440420473928468,
0.000424616565866307, 0.000417156794102717, 0.000453102696396517,
0.000429420158272163, 0.000426339236438714, 0.000424204011080916,
0.000450812884669126, 0.00048728803860348, 0.000526461561504051,
0.000494106517096305, 0.000493488610269819, 0.00041740609044358,
0.000329604373072286, 0.000321981688032803, 0.000313820182149535,
0.000324018084037671, 0.000321928021838835, 0.000325886279909115,
0.000324905583026473, 0.000323263064904554, 0.000315582726878559,
0.00033115144688, 0.000326176783596685, 0.000320421043513733,
0.000317459171547033, 0.000306378296724892, 0.000304230982248009,
0.00031578316067723, 0.000315783239223671, 0.000308443856342272,
0.000302387474982801, 0.000302702247056619, 0.000300580888361005,
0.000298662388842681, 0.000295031270763261, 0.000300955138678924,
0.000308009865186193, 0.00031004060452567, 0.000308580481883199,
0.000309081734643359, 0.000309347430761987, 0.00031610525741575,
0.000316030523318374), X7 = c(0.652246323794644, 0.633658121909502,
0.779640005424855, 0.37849713571782, -0.25338461752528, -0.522937755983531,
-0.124971717359712, -0.93339126000489, -0.957987757878853,
-1.50800507959919, -1.67334654587184, -1.6438607078889, -0.962881285608565,
-0.761952591493898, -1.31627449065341, -1.73806924167703,
-2.31894300820554, -2.73548173635386, -2.16326733735933,
-2.18976199245069, -1.70174528306041, -2.05934220840334,
-1.83997395786783, -1.66507504806585, -1.90235222119862,
-1.84142082212644, -1.77786289146221, -1.23082082088862,
-1.53821065097434, -1.56812844617651, -1.59984630224514,
-1.15906488048868, -1.18713323505712, -0.57100200805735,
-0.228511848060405, 0.253315947225092, 0.0532312338057499,
0.223428854601438, -0.00144862031236322, -0.0398092612664749,
-0.0588761129681466, -0.0771471433941018, 0.117493259867833,
0.459197464971977, 0.292335416896516, 0.114242276938583,
0.202233666501737, 0.331679162319782, 0.175470525372577),
X8 = c(-0.975973892452776, -0.861258722930479, -0.477565916680931,
-0.33185021650311, -0.647129621511124, -0.752564004048953,
-0.620377457915967, -1.30234931058582, -0.793283548875438,
-1.16602371553076, -1.20919980603818, -1.06429334017641,
0.0115994967476536, 1.15288649261686, 0.559547871095353,
0.142148842103466, -0.504045772338972, -1.18889934824633,
0.593184249578906, 0.999978721521271, 1.3874173248314, 0.798117587221041,
0.799993914873595, 0.748516271275043, 0.617354487136954,
0.66965220108721, 0.886228797393692, 1.10466660545966, 0.37553937583687,
0.113225726134138, 0.35930466268218, 0.588184742745078, 0.536929080120224,
0.875533406569582, 1.29869394121533, 1.3954069386108, 1.2896602501424,
1.28952719595234, 1.04620293691641, 1.04835604946992, 0.944659231862545,
0.658278997146449, 0.530493113505759, 0.386873773618166,
0.412971784704427, 0.116661980563476, 0.274658721324867,
-0.231454627137936, -0.0490920485204462), X9 = c(-0.0145637619761308,
-0.0147930666936059, -0.0147662947883565, -0.0325177535962596,
-0.0128572105783758, -0.0126204141222343, -0.0193506283734377,
-0.0204879399440294, -0.015817519858536, -0.0171950326442131,
-0.0146419066547099, -0.0104033279055883, -0.00702994914474458,
0.012163515055523, 0.0311052843018782, 0.0466972362285693,
0.0638538376999092, 0.0587189119891982, -0.0382761719744182,
0.017369814913167, 0.00894072522957917, 0.0159165549773805,
-0.00278974160229076, 0.00087423648615031, -0.000799924570855513,
-0.00720654828839435, 0.00144205696290743, -0.00744120420924264,
-0.00623238593684327, 0.0140296542021987, 0.0078581351311665,
0.0194264610155223, 0.0214019946797077, 0.0135050903487779,
0.0136275994696147, 0.0191532733906993, 0.0145192162284441,
0.0180836191687308, 0.0211148970828572, 0.0228082176297255,
0.0282007760760499, 0.0246107996585935, 0.0169675399817873,
0.0207974225382235, 0.0246684486060467, 0.0216975706632474,
0.0291883466992148, 0.0253376536386199, 0.0262304363854249
), X10 = c(0.225525775667678, 0.218320938251066, 0.205736199627003,
0.18430441198689, 0.171653942908123, 0.169720213220954, 0.169160112065287,
0.162051896119269, 0.158642308835919, 0.163696353319601,
0.162160340961121, 0.160663336867733, 0.116728767616044,
0.0744707092081847, 0.0816373143043771, 0.120472162777503,
0.120923040165681, 0.10545848677116, 0.172548378854213, 0.184998553367414,
0.181410472295269, 0.248454355918383, 0.218320516349615,
0.216853385184294, 0.180488106442423, 0.177799948562715,
0.186118707334448, 0.177473777344988, 0.161807804965392,
0.167723342514708, 0.158568160654393, 0.16773089626074, 0.16307716529608,
0.162099891399657, 0.15812964617053, 0.149690169923917, 0.164750142363451,
0.163460075128328, 0.155183021202297, 0.14616757689419, 0.154376710865108,
0.147389439599357, 0.183009449087266, 0.175519602867033,
0.177732643891337, 0.17814584046646, 0.175149718955584, 0.176134687816332,
0.175399285149764), X11 = c(0.510204081632653, 0.520408163265306,
0.520408163265306, 0.530612244897959, 0.530612244897959,
0.520408163265306, 0.520408163265306, 0.520408163265306,
0.530612244897959, 0.520408163265306, 0.530612244897959,
0.530612244897959, 0.540816326530612, 0.530612244897959,
0.520408163265306, 0.530612244897959, 0.530612244897959,
0.530612244897959, 0.536082474226804, 0.536082474226804,
0.536082474226804, 0.530612244897959, 0.525773195876289,
0.536082474226804, 0.510204081632653, 0.520408163265306,
0.520408163265306, 0.530612244897959, 0.530612244897959,
0.520408163265306, 0.510204081632653, 0.510204081632653,
0.510204081632653, 0.510204081632653, 0.510204081632653,
0.5, 0.5, 0.489795918367347, 0.489795918367347, 0.489795918367347,
0.5, 0.5, 0.510204081632653, 0.5, 0.5, 0.510204081632653,
0.510204081632653, 0.520408163265306, 0.520408163265306)), row.names = c(NA,
-49L), class = c("tbl_df", "tbl", "data.frame")), in_id = 1:20,
out_id = 21L, id = structure(list(id = "Slice01"), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame"))), class = c("rsplit",
"rof_split")), structure(list(data = structure(list(date = structure(c(17095,
17098, 17099, 17100, 17101, 17102, 17105, 17106, 17107, 17108,
17109, 17112, 17113, 17114, 17115, 17116, 17119, 17120, 17121,
17122, 17123, 17126, 17127, 17128, 17130, 17133, 17134, 17135,
17136, 17137, 17140, 17141, 17142, 17143, 17144, 17147, 17148,
17149, 17150, 17151, 17154, 17155, 17156, 17157, 17158, 17162,
17163, 17164, 17165), class = "Date"), ID = c("CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1"), var1 = c(799.369995,
813.109985, 807.669983, 799.070007, 795.349976, 795.369995, 784.539978,
783.609985, 768.700012, 762.130005, 762.02002, 782.52002, 790.51001,
785.309998, 762.559998, 754.02002, 736.080017, 758.48999, 764.47998,
771.22998, 760.539978, 769.200012, 768.27002, 760.98999, 761.679993,
768.23999, 770.840027, 758.039978, 747.919983, 750.5, 762.52002,
759.109985, 771.190002, 776.419983, 789.289978, 789.27002, 796.099976,
797.070007, 797.849976, 790.799988, 794.200012, 796.419983, 794.559998,
791.26001, 789.909973, 791.549988, 785.049988, 782.789978, 771.820007
), var2 = c(0.00301143592272179, 0.0171885235697395, -0.00669036428079295,
-0.0106478836418512, -0.00465545067066953, 0.0000251700516804565,
-0.0136163258207899, -0.00118539912060411, -0.0190272881732103,
-0.00854690633203736, -0.000144312649125955, 0.0269021803390415,
0.0102105886057713, -0.00657804700031572, -0.0289694516279417,
-0.0111990899370517, -0.0237924756958046, 0.0304450229355975,
0.00789725649510542, 0.0088295314155904, -0.0138609782778413,
0.0113866913646978, -0.0012090379426567, -0.00947587412040363,
0.00090671757719174, 0.00861253683999563, 0.00338440726054889,
-0.016605324777718, -0.0133502127773003, 0.00344958960669994,
0.0160160159893405, -0.00447205963195563, 0.0159133949476373,
0.00678170228664343, 0.0165760738798502, -0.0000252860172512692,
0.00865350998635406, 0.00121847887105075, 0.000978545163097477,
-0.00883623264030775, 0.00429947401567232, 0.00279522911918573,
-0.00233543235943645, -0.00415322695366804, -0.00170618631415476,
0.00207620495506755, -0.00821173659091756, -0.00287881031086645,
-0.0140139389980795), Y = c(1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1,
1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0,
1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0), Y_plus_1 = c(0,
1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0,
1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0,
0, 0, 0, 1, 0, 0), X1 = c(4.13858526513854, 4.13858526513855,
4.16341131085939, 4.16341131085937, 4.16341131085937, 4.11423119297315,
4.13858526513857, 3.97599968560627, 5.99758130881283, 8.41953801047614,
7.95231443679086, 7.88558780320248, 7.6408950559188, 4.51370117323327,
4.52868963859669, 4.39998987943623, 4.18852747359839, 4.27042958796773,
2.30720560360487, 2.3083029424251, 2.3083029424251, 2.30720560360488,
2.30720560360486, 2.33467572807035, 2.33467572807036, 2.33467572807036,
2.30720560360486, 2.33467572807035, 2.31545097851707, 2.3399413414153,
2.40685890963718, 2.40309440701756, 2.33633188340289, 2.3363318834029,
2.23996107961566, 2.23996107961567, 2.23996107961566, 3.14644299189703,
3.1910343925295, 3.4393907031427, 3.30413087760388, 3.33080017630688,
2.63827508869038, 2.99443088216722, 2.99443088216723, 2.99443088216722,
2.99443088216722, 3.14542139469794, 3.14542139469794), X2 = c(18,
17, 16, 15, 14, 13, 12, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15,
14, 13, 12, 11, 10, 10, 10, 10, 10, 87, 86, 85, 84, 83, 82, 81
), X3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), X4 = c(1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
), X5 = c(0.0769944316144198, 0.0793311568823971, 0.0803743155230278,
0.0625789438826206, 0.0586506192715035, 0.0610303101083243, 0.0703939970975855,
0.0867098273608016, 0.0673120522212106, 0.0877296725069155, 0.0923742675361241,
0.0821050363880187, 0.0360531976099817, 0.063410467337928, 0.0289807505667197,
0.0403890038946993, 0.0587200889534704, 0.124855015077667, 0.0636602113103218,
0.0748772236055617, 0.0828248414842617, 0.0588561607897347, 0.0437146614571738,
0.0399432627968126, 0.0535895503558405, 0.0538598239712004, 0.0600971764378981,
0.0543532803438423, 0.0412119504402689, 0.0445593481900316, 0.0471324573693227,
0.0366910541674913, 0.0412784111781792, 0.0338162772274317, 0.0391189676384125,
0.051151724195942, 0.0442197922283997, 0.0458769828703159, 0.0392536462039503,
0.0397989336000519, 0.0293505218180493, 0.0229058449521028, 0.0154019371887762,
0.0102366640366435, 0.00783792657548366, 0.014037818210456, 0.00900392496961011,
0.0148108452415051, 0.0159871581537364), X6 = c(0.000428660536007568,
0.000424348382531349, 0.000403672086504106, 0.000425772306880377,
0.000441567036819891, 0.000440420473928468, 0.000424616565866307,
0.000417156794102717, 0.000453102696396517, 0.000429420158272163,
0.000426339236438714, 0.000424204011080916, 0.000450812884669126,
0.00048728803860348, 0.000526461561504051, 0.000494106517096305,
0.000493488610269819, 0.00041740609044358, 0.000329604373072286,
0.000321981688032803, 0.000313820182149535, 0.000324018084037671,
0.000321928021838835, 0.000325886279909115, 0.000324905583026473,
0.000323263064904554, 0.000315582726878559, 0.00033115144688,
0.000326176783596685, 0.000320421043513733, 0.000317459171547033,
0.000306378296724892, 0.000304230982248009, 0.00031578316067723,
0.000315783239223671, 0.000308443856342272, 0.000302387474982801,
0.000302702247056619, 0.000300580888361005, 0.000298662388842681,
0.000295031270763261, 0.000300955138678924, 0.000308009865186193,
0.00031004060452567, 0.000308580481883199, 0.000309081734643359,
0.000309347430761987, 0.00031610525741575, 0.000316030523318374
), X7 = c(0.652246323794644, 0.633658121909502, 0.779640005424855,
0.37849713571782, -0.25338461752528, -0.522937755983531, -0.124971717359712,
-0.93339126000489, -0.957987757878853, -1.50800507959919, -1.67334654587184,
-1.6438607078889, -0.962881285608565, -0.761952591493898, -1.31627449065341,
-1.73806924167703, -2.31894300820554, -2.73548173635386, -2.16326733735933,
-2.18976199245069, -1.70174528306041, -2.05934220840334, -1.83997395786783,
-1.66507504806585, -1.90235222119862, -1.84142082212644, -1.77786289146221,
-1.23082082088862, -1.53821065097434, -1.56812844617651, -1.59984630224514,
-1.15906488048868, -1.18713323505712, -0.57100200805735, -0.228511848060405,
0.253315947225092, 0.0532312338057499, 0.223428854601438, -0.00144862031236322,
-0.0398092612664749, -0.0588761129681466, -0.0771471433941018,
0.117493259867833, 0.459197464971977, 0.292335416896516, 0.114242276938583,
0.202233666501737, 0.331679162319782, 0.175470525372577), X8 = c(-0.975973892452776,
-0.861258722930479, -0.477565916680931, -0.33185021650311, -0.647129621511124,
-0.752564004048953, -0.620377457915967, -1.30234931058582, -0.793283548875438,
-1.16602371553076, -1.20919980603818, -1.06429334017641, 0.0115994967476536,
1.15288649261686, 0.559547871095353, 0.142148842103466, -0.504045772338972,
-1.18889934824633, 0.593184249578906, 0.999978721521271, 1.3874173248314,
0.798117587221041, 0.799993914873595, 0.748516271275043, 0.617354487136954,
0.66965220108721, 0.886228797393692, 1.10466660545966, 0.37553937583687,
0.113225726134138, 0.35930466268218, 0.588184742745078, 0.536929080120224,
0.875533406569582, 1.29869394121533, 1.3954069386108, 1.2896602501424,
1.28952719595234, 1.04620293691641, 1.04835604946992, 0.944659231862545,
0.658278997146449, 0.530493113505759, 0.386873773618166, 0.412971784704427,
0.116661980563476, 0.274658721324867, -0.231454627137936, -0.0490920485204462
), X9 = c(-0.0145637619761308, -0.0147930666936059, -0.0147662947883565,
-0.0325177535962596, -0.0128572105783758, -0.0126204141222343,
-0.0193506283734377, -0.0204879399440294, -0.015817519858536,
-0.0171950326442131, -0.0146419066547099, -0.0104033279055883,
-0.00702994914474458, 0.012163515055523, 0.0311052843018782,
0.0466972362285693, 0.0638538376999092, 0.0587189119891982, -0.0382761719744182,
0.017369814913167, 0.00894072522957917, 0.0159165549773805, -0.00278974160229076,
0.00087423648615031, -0.000799924570855513, -0.00720654828839435,
0.00144205696290743, -0.00744120420924264, -0.00623238593684327,
0.0140296542021987, 0.0078581351311665, 0.0194264610155223, 0.0214019946797077,
0.0135050903487779, 0.0136275994696147, 0.0191532733906993, 0.0145192162284441,
0.0180836191687308, 0.0211148970828572, 0.0228082176297255, 0.0282007760760499,
0.0246107996585935, 0.0169675399817873, 0.0207974225382235, 0.0246684486060467,
0.0216975706632474, 0.0291883466992148, 0.0253376536386199, 0.0262304363854249
), X10 = c(0.225525775667678, 0.218320938251066, 0.205736199627003,
0.18430441198689, 0.171653942908123, 0.169720213220954, 0.169160112065287,
0.162051896119269, 0.158642308835919, 0.163696353319601, 0.162160340961121,
0.160663336867733, 0.116728767616044, 0.0744707092081847, 0.0816373143043771,
0.120472162777503, 0.120923040165681, 0.10545848677116, 0.172548378854213,
0.184998553367414, 0.181410472295269, 0.248454355918383, 0.218320516349615,
0.216853385184294, 0.180488106442423, 0.177799948562715, 0.186118707334448,
0.177473777344988, 0.161807804965392, 0.167723342514708, 0.158568160654393,
0.16773089626074, 0.16307716529608, 0.162099891399657, 0.15812964617053,
0.149690169923917, 0.164750142363451, 0.163460075128328, 0.155183021202297,
0.14616757689419, 0.154376710865108, 0.147389439599357, 0.183009449087266,
0.175519602867033, 0.177732643891337, 0.17814584046646, 0.175149718955584,
0.176134687816332, 0.175399285149764), X11 = c(0.510204081632653,
0.520408163265306, 0.520408163265306, 0.530612244897959, 0.530612244897959,
0.520408163265306, 0.520408163265306, 0.520408163265306, 0.530612244897959,
0.520408163265306, 0.530612244897959, 0.530612244897959, 0.540816326530612,
0.530612244897959, 0.520408163265306, 0.530612244897959, 0.530612244897959,
0.530612244897959, 0.536082474226804, 0.536082474226804, 0.536082474226804,
0.530612244897959, 0.525773195876289, 0.536082474226804, 0.510204081632653,
0.520408163265306, 0.520408163265306, 0.530612244897959, 0.530612244897959,
0.520408163265306, 0.510204081632653, 0.510204081632653, 0.510204081632653,
0.510204081632653, 0.510204081632653, 0.5, 0.5, 0.489795918367347,
0.489795918367347, 0.489795918367347, 0.5, 0.5, 0.510204081632653,
0.5, 0.5, 0.510204081632653, 0.510204081632653, 0.520408163265306,
0.520408163265306)), row.names = c(NA, -49L), class = c("tbl_df",
"tbl", "data.frame")), in_id = 2:21, out_id = 22L, id = structure(list(
id = "Slice02"), row.names = c(NA, -1L), class = c("tbl_df",
"tbl", "data.frame"))), class = c("rsplit", "rof_split"))), id = c("Slice01",
"Slice02")), row.names = 1:2, class = c("rset", "tbl_df", "tbl",
"data.frame"), initial = 20, assess = 1, cumulative = FALSE, skip = 0)

We don't need the . and map. After the select, it can be converted to list with as.list (if it is required), otherwise, just keep it as the tibble
library(purrr)
library(dplyr)
map(dat$splits, ~ analysis(.x) %>%
as_tibble(., .name_repair = "universal") %>%
select(X1:X9) %>%
as.list)

Related

Collapsing daily longitudinal data into monthly observations by ID in R

I have longitudinal data with >100 rows per subject representing daily observations. I want to collapse columns, by subject ID, into monthly observations (i.e. have multiple rows per ID that are summarizing every 30 rows (days) of data).
How can you specify such groupings of days using dplyr?
Also of note, all subjects have different total number of days
Edit: data sample below
df<-structure(list(ID = structure(c(100087, 100087, 100087, 100087,
100087, 100087, 100087, 100087, 100087, 100087, 100087, 100087,
100087, 100087, 100087, 100087, 100087, 100087, 100087, 100087)), time = structure(c(0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19)),
BMI = structure(c(20.06, 20.06, 20.06, 20.06, 20.06, 20.06,
20.06, 20.06, 20.06, 20.06, 20.06, 20.06, 20.06, 20.06, 20.06,
20.06, 20.06, 20.06, 20.06, 20.06)), Dis = structure(c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)),
Drug1 = structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1)), Drug2 = structure(c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))
I would group_by ID and a new times variable where you can specify the bins with time %/% 30 for 30 rows. Since your example data has only a few rows I set this to 5. Since each respondent has a different amount of times we need to record first_time and last_time to then overwrite times as x - y times where x and y are first and last time.
In the across call you would need to specify the way you want to aggregate the data, below I choose mean. In case you want to get the mean of BMI and the max value of Drug1 you would need to specify each column in a separate function call.
library(dplyr)
df %>%
group_by(ID, times = time %/% 5) %>%
summarise(across(BMI:Drug2, mean),
time_first = first(time),
time_last = last(time)
) %>%
ungroup() %>%
mutate(times = paste0(time_first, "-", time_last)) %>%
select(-c(time_first, time_last))
#> `summarise()` has grouped output by 'ID'. You can override using the `.groups`
#> argument.
#> # A tibble: 4 × 6
#> ID times BMI Dis Drug1 Drug2
#> <dbl> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 100087 0-4 20.1 0 1 1
#> 2 100087 5-9 20.1 0 1 1
#> 3 100087 10-14 20.1 0 1 1
#> 4 100087 15-19 20.1 0 1 1
# OPs data
df <- structure(list(ID = structure(c(100087, 100087, 100087, 100087, 100087, 100087, 100087, 100087, 100087, 100087, 100087, 100087, 100087, 100087, 100087, 100087, 100087, 100087, 100087, 100087)), time = structure(c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19)), BMI = structure(c(20.06, 20.06, 20.06, 20.06, 20.06, 20.06, 20.06, 20.06, 20.06, 20.06, 20.06, 20.06, 20.06, 20.06, 20.06, 20.06, 20.06, 20.06, 20.06, 20.06)), Dis = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), Drug1 = structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), Drug2 = structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"))
Created on 2022-09-27 by the reprex package (v0.3.0)

How can I represent one column's values using multiple columns in R where one new column is conditional?

Looking at similar questions, I could not find one that matched my need.
If one does contain a solution, please share its link.
I have this dput-produced data:
structure(list(Player = c("Seth Lugo", "Jacob deGrom", "Rick Porcello",
"David Peterson", "Michael Wacha", "Seth Lugo", "Jacob deGrom",
"Rick Porcello", "David Peterson", "Steven Matz", "Seth Lugo",
"Jacob deGrom", "Rick Porcello", "David Peterson", "Seth Lugo",
"Jacob deGrom", "Rick Porcello", "Michael Wacha", "David Peterson",
"Jacob deGrom", "Seth Lugo", "Rick Porcello", "Robert Gsellman",
"Michael Wacha", "Ariel Jurado", "Jacob deGrom", "Rick Porcello",
"Seth Lugo", "Robert Gsellman", "David Peterson"), Date = structure(c(1601164800,
1601078400, 1601078400, 1600905600, 1600819200, 1600732800, 1600646400,
1600560000, 1600473600, 1600387200, 1600300800, 1600214400, 1600128000,
1599955200, 1599868800, 1599782400, 1599609600, 1599523200, 1599436800,
1599350400, 1599264000, 1599177600, 1599091200, 1599004800, 1598918400,
1598832000, 1598745600, 1598745600, 1598659200, 1598572800), tzone = "UTC", class = c("POSIXct",
"POSIXt")), DblHdr = c(0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 2), DateStr = c("09/27/2020",
"09/26/2020", "09/26/2020", "09/24/2020", "09/23/2020", "09/22/2020",
"09/21/2020", "09/20/2020", "09/19/2020", "09/18/2020", "09/17/2020",
"09/16/2020", "09/15/2020", "09/13/2020", "09/12/2020", "09/11/2020",
"09/09/2020", "09/08/2020", "09/07/2020", "09/06/2020", "09/05/2020",
"09/04/2020", "09/03/2020", "09/02/2020", "09/01/2020", "08/31/2020",
"08/30/2020", "08/30/2020", "08/29/2020", "08/28/2020"), Month = c("09",
"09", "09", "09", "09", "09", "09", "09", "09", "09", "09", "09",
"09", "09", "09", "09", "09", "09", "09", "09", "09", "09", "09",
"09", "09", "08", "08", "08", "08", "08"), Tm = c("NYM", "NYM",
"NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM",
"NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM",
"NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM",
"NYM"), Opp = c("WSN", "WSN", "WSN", "WSN", "TBR", "TBR", "TBR",
"ATL", "ATL", "ATL", "PHI", "PHI", "PHI", "TOR", "TOR", "TOR",
"BAL", "BAL", "PHI", "PHI", "PHI", "PHI", "NYY", "BAL", "BAL",
"MIA", "NYY", "NYY", "NYY", "NYY"), Rslt = c("L 5-15", "L 3-4",
"L 3-5", "W 3-2", "L 5-8", "W 5-2", "L 1-2", "L 0-7", "W 7-2",
"L 2-15", "W 10-6", "W 5-4", "L 1-4", "L 3-7", "L 2-3", "W 18-1",
"W 7-6", "L 2-11", "L 8-9", "W 14-1", "W 5-1", "L 3-5", "W 9-7",
"W 9-4", "L 5-9", "L 3-5", "L 7-8", "L 2-5", "L 1-2", "W 4-3"
), W_L = c("L", "L", "L", "W", "L", "W", "L", "L", "W", "L",
"W", "W", "L", "L", "L", "W", "W", "L", "L", "W", "W", "L", "W",
"W", "L", "L", "L", "L", "L", "W"), temp = c("L 5", "L 3", "L 3",
"W 3", "L 5", "W 5", "L 1", "L 0", "W 7", "L 2", "W 10", "W 5",
"L 1", "L 3", "L 2", "W 18", "W 7", "L 2", "L 8", "W 14", "W 5",
"L 3", "W 9", "W 9", "L 5", "L 3", "L 7", "L 2", "L 1", "W 4"
), RS = c(5, 3, 3, 3, 5, 5, 1, 0, 7, 2, 10, 5, 1, 3, 2, 18, 7,
2, 8, 14, 5, 3, 9, 9, 5, 3, 7, 2, 1, 4), RA = c(15, 4, 5, 2,
8, 2, 2, 7, 2, 15, 6, 4, 4, 7, 3, 1, 6, 11, 9, 1, 1, 5, 7, 4,
9, 5, 8, 5, 2, 3), Rdiff = c(-10, -1, -2, 1, -3, 3, -1, -7, 5,
-13, 4, 1, -3, -4, -1, 17, 1, -9, -1, 13, 4, -2, 2, 5, -4, -2,
-1, -3, -1, 1), absV = c(10, 1, 2, 1, 3, 3, 1, 7, 5, 13, 4, 1,
3, 4, 1, 17, 1, 9, 1, 13, 4, 2, 2, 5, 4, 2, 1, 3, 1, 1), App_Dec = c("GS-2, L",
"GS-5", "GS-3, L", "GS-7, W", "GS-6, L", "GS-7, W", "GS-7, L",
"GS-7, L", "GS-6, W", "GS-3, L", "GS-2", "GS-2", "GS-6, L", "GS-5, L",
"GS-6, L", "GS-6, W", "GS-4", "GS-4, L", "GS-2", "GS-7, W", "GS-5, W",
"GS-6", "GS-2", "GS-3", "GS-4", "GS-6, L", "GS-5", "GS-4", "GS-4",
"GS-4"), IP = c(1.1, 5, 3, 7, 6, 6.1, 7, 7, 6, 2.2, 1.2, 2, 6,
5, 5.1, 6, 4, 4, 2, 7, 5, 6, 1.2, 3, 4, 6, 5, 3.2, 4, 4), H = c(5,
5, 8, 4, 6, 4, 4, 3, 3, 8, 8, 4, 6, 3, 7, 3, 10, 7, 3, 3, 4,
3, 4, 4, 9, 6, 4, 4, 4, 4), R = c(6, 3, 5, 1, 4, 2, 2, 1, 1,
6, 6, 3, 4, 2, 3, 1, 5, 5, 5, 1, 1, 2, 4, 2, 5, 4, 2, 1, 1, 3
), ER = c(6, 3, 3, 1, 4, 1, 2, 1, 1, 6, 6, 3, 4, 2, 3, 1, 5,
4, 5, 1, 1, 2, 4, 2, 5, 1, 2, 1, 1, 3), BB = c(2, 2, 1, 1, 0,
1, 2, 2, 4, 3, 0, 1, 2, 2, 1, 2, 0, 0, 4, 2, 2, 2, 4, 1, 0, 2,
2, 2, 0, 3), SO = c(1, 10, 3, 4, 4, 7, 14, 10, 10, 5, 3, 1, 5,
2, 5, 9, 3, 3, 3, 12, 8, 6, 0, 2, 2, 9, 2, 7, 4, 3), HR = c(0,
2, 1, 0, 2, 1, 1, 1, 1, 2, 4, 0, 1, 1, 0, 0, 0, 2, 1, 1, 1, 0,
0, 0, 1, 1, 0, 1, 1, 0), UER = c(0, 0, 2, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0),
Pit = c(38, 113, 67, 107, 66, 95, 112, 100, 102, 76, 52,
40, 94, 81, 91, 102, 66, 71, 70, 108, 81, 100, 52, 69, 84,
103, 86, 60, 57, 70), Str = c(24, 78, 42, 68, 45, 66, 70,
70, 62, 45, 30, 25, 66, 52, 60, 68, 45, 49, 37, 74, 50, 65,
22, 41, 53, 72, 55, 39, 33, 37), GSc = c(19, 53, 29, 68,
48, 65, 73, 75, 68, 20, 18, 36, 47, 53, 46, 69, 25, 33, 29,
77, 61, 62, 27, 44, 26, 57, 51, 54, 54, 42), BF = c(12, 22,
19, 26, 23, 24, 26, 26, 24, 18, 14, 11, 26, 20, 24, 23, 21,
20, 14, 26, 21, 23, 13, 15, 21, 27, 20, 16, 15, 18), AB = c(8,
20, 18, 24, 23, 23, 23, 23, 20, 15, 13, 9, 24, 18, 22, 21,
21, 20, 9, 24, 19, 21, 8, 13, 20, 25, 18, 14, 15, 15), H2B = c(2,
0, 1, 1, 1, 0, 2, 0, 2, 2, 1, 2, 1, 0, 2, 1, 1, 1, 1, 1,
0, 0, 1, 0, 2, 2, 2, 0, 1, 0), H3B = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 1, 0), IBB = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0),
HBP = c(1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0), SH = c(0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0), SF = c(1, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0), GDP = c(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1), SB = c(0, 1,
1, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 2, 0,
1, 0, 0, 0, 3, 0, 0, 0, 0), CS = c(0, 0, 0, 0, 1, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0), PO = c(0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), BK = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0), WP = c(0, 1, 1, 1, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 1, 0, 0), ERA = c("40.5", "5.4", "9", "1.29", "6", "1.42",
"2.57", "1.29", "1.5", "20.25", "32.4", "13.5", "6", "3.6",
"5.0599999999999996", "1.5", "11.25", "9", "22.5", "1.29",
"1.8", "3", "21.6", "6", "11.25", "1.5", "3.6", "2.4500000000000002",
"2.25", "6.75"), WPA = c(-0.471, -0.087, -0.256, 0.34, -0.22,
0.18, 0.107, 0.219, 0.229, -0.358, -0.487, -0.186, -0.156,
0.036, -0.047, 0.049, -0.329, -0.321, -0.34, 0.193, 0.156,
0.07, -0.312, -0.042, -0.278, -0.271, 0.029, 0.02, 0.092,
-0.174), RE24 = c(-5.122, -0.193, -3.316, 2.931, -1.08, 1.509,
1.406, 2.406, 1.92, -4.641, -5.444, -1.919, -0.758, 0.679,
0.245, 2.215, -3.054, -3.054, -4.027, 2.406, 1.433, 0.92,
-3.788, -0.359, -2.812, -1.08, 0.707, 0.364, 1.166, -0.834
), aLI = c(1.45, 1.244, 0.974, 1.271, 0.965, 0.921, 0.955,
0.888, 1.066, 0.962, 0.767, 1.073, 0.941, 0.852, 1.353, 0.392,
0.857, 0.805, 0.904, 0.75, 1.037, 0.861, 1.232, 1.355, 0.914,
1.239, 1.213, 1.28, 0.748, 1.407)), row.names = c(NA, -30L
), class = c("tbl_df", "tbl", "data.frame"))
Desired output:
The numbers starting in the second column are the total absV values for each player for each column. The last column contains the sum of all the absV values for each player where absV > 5. Only a sample of the first 3 rows are shown, and the absV values are just filler numbers.
| Player | 1 | 2 | 3 | 4 | 5 | >5 |
| deGrom | 2 | 3 | 5 | 0 | 1 | 3 |
| Matz | 2 | 3 | 5 | 0 | 1 | 3 |
Code tried (I need help getting beyond the point shown). I would prefer if the code uses dplyr:
starter %>%
select(Player, absV) %>%
group_by(Player, absV) %>%
summarize(numG= n()) %>%
arrange(Player,absV)
To do this you to bifurcate your data with rows per player >5 and <=5, then rbind them together and thereafter pivot_wider. Follow this code
library(dplyr)
library(tidyr)
df <- starter %>% group_by(Player) %>%
mutate(row = row_number()) %>%
select(Player, absV, row) %>% arrange(Player)
df %>% filter(row <= 5) %>%
mutate(row = as.character(row)) %>%
rbind(df %>% filter(row > 5) %>%
summarise( absV = sum(absV)) %>%
mutate(row = ">5")) %>%
pivot_wider(id_cols = Player, names_from = row, values_from = absV)
# A tibble: 8 x 7
# Groups: Player [8]
Player `1` `2` `3` `4` `5` `>5`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Ariel Jurado 4 NA NA NA NA NA
2 David Peterson 1 5 4 1 1 NA
3 Jacob deGrom 1 1 1 17 13 2
4 Michael Wacha 3 9 5 NA NA NA
5 Rick Porcello 2 7 3 1 2 1
6 Robert Gsellman 2 1 NA NA NA NA
7 Seth Lugo 10 3 4 1 4 3
8 Steven Matz 13 NA NA NA NA NA
Note. Loading tidyverse package, at once, directly is advised.
Note-2 If you still want to sort absV before changing the data-format, add absV in arrange syntax beforehand joining them..
df <- starter %>% group_by(Player) %>%
arrange(Player, absV) %>%
mutate(row = row_number()) %>%
select(Player, absV, row)
df %>% filter(row <= 5) %>%
mutate(row = as.character(row)) %>%
rbind(df %>% filter(row > 5) %>%
summarise( absV = sum(absV)) %>%
mutate(row = ">5")) %>%
pivot_wider(id_cols = Player, names_from = row, values_from = absV)
#this will give the following diff output
# A tibble: 8 x 7
# Groups: Player [8]
Player `1` `2` `3` `4` `5` `>5`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Ariel Jurado 4 NA NA NA NA NA
2 David Peterson 1 1 1 4 5 NA
3 Jacob deGrom 1 1 1 2 13 17
4 Michael Wacha 3 5 9 NA NA NA
5 Rick Porcello 1 1 2 2 3 7
6 Robert Gsellman 1 2 NA NA NA NA
7 Seth Lugo 1 3 3 4 4 10
8 Steven Matz 13 NA NA NA NA NA
Additional Question in comments below
Follow this code to work out frequency of each absV
df %>% group_by(Player, absV) %>% mutate(freq = n()) %>% ungroup()
#check it
df %>% group_by(Player, absV) %>% mutate(freq = n()) %>% ungroup() %>% select(Player, absV, freq)
Player absV freq
<chr> <dbl> <int>
1 Seth Lugo 10 1
2 Jacob deGrom 1 3
3 Rick Porcello 2 2
4 David Peterson 1 3
5 Michael Wacha 3 1
6 Seth Lugo 3 2
7 Jacob deGrom 1 3
8 Rick Porcello 7 1
9 David Peterson 5 1
10 Steven Matz 13 1
# ... with 20 more rows
Using data.table
library(data.table)
dcast(setDT(starter), Player ~ rowid(Player), value.var = 'absV')

map a custom function to certain columns in a list of tibbles

I am trying to apply map to scale a number of columns, the data looks like:
# A tibble: 20 x 18
date ID var1 var2 Y Y_plus_1 X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
<date> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2016-10-28 CAT1 795. 2.52e-5 0 0 4.11 13 0 1 0.0610 4.40e-4 -0.523 -0.753 -1.26e-2 0.170
2 2016-10-31 CAT1 785. -1.36e-2 0 0 4.14 12 0 1 0.0704 4.25e-4 -0.125 -0.620 -1.94e-2 0.169
3 2016-11-01 CAT1 784. -1.19e-3 1 0 3.98 11 0 1 0.0867 4.17e-4 -0.933 -1.30 -2.05e-2 0.162
4 2016-11-02 CAT1 769. -1.90e-2 0 1 6.00 10 0 1 0.0673 4.53e-4 -0.958 -0.793 -1.58e-2 0.159
5 2016-11-03 CAT1 762. -8.55e-3 0 0 8.42 10 0 1 0.0877 4.29e-4 -1.51 -1.17 -1.72e-2 0.164
6 2016-11-04 CAT1 762. -1.44e-4 1 0 7.95 10 0 1 0.0924 4.26e-4 -1.67 -1.21 -1.46e-2 0.162
7 2016-11-07 CAT1 783. 2.69e-2 1 1 7.89 10 0 1 0.0821 4.24e-4 -1.64 -1.06 -1.04e-2 0.161
8 2016-11-08 CAT1 791. 1.02e-2 1 1 7.64 10 0 1 0.0361 4.51e-4 -0.963 0.0116 -7.03e-3 0.117
9 2016-11-09 CAT1 785. -6.58e-3 1 1 4.51 10 0 1 0.0634 4.87e-4 -0.762 1.15 1.22e-2 0.0745
10 2016-11-10 CAT1 763. -2.90e-2 0 1 4.53 10 0 1 0.0290 5.26e-4 -1.32 0.560 3.11e-2 0.0816
11 2016-11-11 CAT1 754. -1.12e-2 0 0 4.40 10 0 1 0.0404 4.94e-4 -1.74 0.142 4.67e-2 0.120
12 2016-11-14 CAT1 736. -2.38e-2 0 0 4.19 10 0 1 0.0587 4.93e-4 -2.32 -0.504 6.39e-2 0.121
13 2016-11-15 CAT1 758. 3.04e-2 1 0 4.27 10 0 1 0.125 4.17e-4 -2.74 -1.19 5.87e-2 0.105
14 2016-11-16 CAT1 764. 7.90e-3 1 1 2.31 29 0 1 0.0637 3.30e-4 -2.16 0.593 -3.83e-2 0.173
15 2016-11-17 CAT1 771. 8.83e-3 1 1 2.31 28 0 1 0.0749 3.22e-4 -2.19 1.000 1.74e-2 0.185
16 2016-11-18 CAT1 761. -1.39e-2 0 1 2.31 27 0 1 0.0828 3.14e-4 -1.70 1.39 8.94e-3 0.181
17 2016-11-21 CAT1 769. 1.14e-2 1 0 2.31 26 0 1 0.0589 3.24e-4 -2.06 0.798 1.59e-2 0.248
18 2016-11-22 CAT1 768. -1.21e-3 0 1 2.31 25 0 1 0.0437 3.22e-4 -1.84 0.800 -2.79e-3 0.218
19 2016-11-23 CAT1 761. -9.48e-3 0 0 2.33 24 0 1 0.0399 3.26e-4 -1.67 0.749 8.74e-4 0.217
20 2016-11-25 CAT1 762. 9.07e-4 0 0 2.33 23 0 1 0.0536 3.25e-4 -1.90 0.617 -8.00e-4 0.180
I have a scale function I would like to apply to all the X variables in each list. I am just wondering how I can apply the scale function to all of these columns in each list.
Function:
Scale_Me <- function(x){
(x - mean(x, na.rm = TRUE)) / sd(x, na.rm = TRUE)
}
Code for one variable:
map(mylist, ~mutate(.x,
scala = Scale_Me(.x[["var1"]])))
Data:
mylist <- list(structure(list(date = structure(c(17095, 17098, 17099, 17100,
17101, 17102, 17105, 17106, 17107, 17108, 17109, 17112, 17113,
17114, 17115, 17116, 17119, 17120, 17121, 17122), class = "Date"),
ID = c("CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1"), var1 = c(799.369995,
813.109985, 807.669983, 799.070007, 795.349976, 795.369995,
784.539978, 783.609985, 768.700012, 762.130005, 762.02002,
782.52002, 790.51001, 785.309998, 762.559998, 754.02002,
736.080017, 758.48999, 764.47998, 771.22998), var2 = c(0.00301143592272179,
0.0171885235697395, -0.00669036428079295, -0.0106478836418512,
-0.00465545067066953, 0.0000251700516804565, -0.0136163258207899,
-0.00118539912060411, -0.0190272881732103, -0.00854690633203736,
-0.000144312649125955, 0.0269021803390415, 0.0102105886057713,
-0.00657804700031572, -0.0289694516279417, -0.0111990899370517,
-0.0237924756958046, 0.0304450229355975, 0.00789725649510542,
0.0088295314155904), Y = c(1, 1, 0, 0, 0, 0, 0, 1, 0, 0,
1, 1, 1, 1, 0, 0, 0, 1, 1, 1), Y_plus_1 = c(0, 1, 1, 0, 0,
0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1), X1 = c(4.13858526513854,
4.13858526513855, 4.16341131085939, 4.16341131085937, 4.16341131085937,
4.11423119297315, 4.13858526513857, 3.97599968560627, 5.99758130881283,
8.41953801047614, 7.95231443679086, 7.88558780320248, 7.6408950559188,
4.51370117323327, 4.52868963859669, 4.39998987943623, 4.18852747359839,
4.27042958796773, 2.30720560360487, 2.3083029424251), X2 = c(18,
17, 16, 15, 14, 13, 12, 11, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 29, 28), X3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0), X4 = c(1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), X5 = c(0.0769944316144198,
0.0793311568823971, 0.0803743155230278, 0.0625789438826206,
0.0586506192715035, 0.0610303101083243, 0.0703939970975855,
0.0867098273608016, 0.0673120522212106, 0.0877296725069155,
0.0923742675361241, 0.0821050363880187, 0.0360531976099817,
0.063410467337928, 0.0289807505667197, 0.0403890038946993,
0.0587200889534704, 0.124855015077667, 0.0636602113103218,
0.0748772236055617), X6 = c(0.000428660536007568, 0.000424348382531349,
0.000403672086504106, 0.000425772306880377, 0.000441567036819891,
0.000440420473928468, 0.000424616565866307, 0.000417156794102717,
0.000453102696396517, 0.000429420158272163, 0.000426339236438714,
0.000424204011080916, 0.000450812884669126, 0.00048728803860348,
0.000526461561504051, 0.000494106517096305, 0.000493488610269819,
0.00041740609044358, 0.000329604373072286, 0.000321981688032803
), X7 = c(0.652246323794644, 0.633658121909502, 0.779640005424855,
0.37849713571782, -0.25338461752528, -0.522937755983531,
-0.124971717359712, -0.93339126000489, -0.957987757878853,
-1.50800507959919, -1.67334654587184, -1.6438607078889, -0.962881285608565,
-0.761952591493898, -1.31627449065341, -1.73806924167703,
-2.31894300820554, -2.73548173635386, -2.16326733735933,
-2.18976199245069), X8 = c(-0.975973892452776, -0.861258722930479,
-0.477565916680931, -0.33185021650311, -0.647129621511124,
-0.752564004048953, -0.620377457915967, -1.30234931058582,
-0.793283548875438, -1.16602371553076, -1.20919980603818,
-1.06429334017641, 0.0115994967476536, 1.15288649261686,
0.559547871095353, 0.142148842103466, -0.504045772338972,
-1.18889934824633, 0.593184249578906, 0.999978721521271),
X9 = c(-0.0145637619761308, -0.0147930666936059, -0.0147662947883565,
-0.0325177535962596, -0.0128572105783758, -0.0126204141222343,
-0.0193506283734377, -0.0204879399440294, -0.015817519858536,
-0.0171950326442131, -0.0146419066547099, -0.0104033279055883,
-0.00702994914474458, 0.012163515055523, 0.0311052843018782,
0.0466972362285693, 0.0638538376999092, 0.0587189119891982,
-0.0382761719744182, 0.017369814913167), X10 = c(0.225525775667678,
0.218320938251066, 0.205736199627003, 0.18430441198689, 0.171653942908123,
0.169720213220954, 0.169160112065287, 0.162051896119269,
0.158642308835919, 0.163696353319601, 0.162160340961121,
0.160663336867733, 0.116728767616044, 0.0744707092081847,
0.0816373143043771, 0.120472162777503, 0.120923040165681,
0.10545848677116, 0.172548378854213, 0.184998553367414),
X11 = c(0.510204081632653, 0.520408163265306, 0.520408163265306,
0.530612244897959, 0.530612244897959, 0.520408163265306,
0.520408163265306, 0.520408163265306, 0.530612244897959,
0.520408163265306, 0.530612244897959, 0.530612244897959,
0.540816326530612, 0.530612244897959, 0.520408163265306,
0.530612244897959, 0.530612244897959, 0.530612244897959,
0.536082474226804, 0.536082474226804)), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
date = structure(c(17098, 17099, 17100, 17101, 17102, 17105,
17106, 17107, 17108, 17109, 17112, 17113, 17114, 17115, 17116,
17119, 17120, 17121, 17122, 17123), class = "Date"), ID = c("CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1"), var1 = c(813.109985, 807.669983,
799.070007, 795.349976, 795.369995, 784.539978, 783.609985,
768.700012, 762.130005, 762.02002, 782.52002, 790.51001,
785.309998, 762.559998, 754.02002, 736.080017, 758.48999,
764.47998, 771.22998, 760.539978), var2 = c(0.0171885235697395,
-0.00669036428079295, -0.0106478836418512, -0.00465545067066953,
0.0000251700516804565, -0.0136163258207899, -0.00118539912060411,
-0.0190272881732103, -0.00854690633203736, -0.000144312649125955,
0.0269021803390415, 0.0102105886057713, -0.00657804700031572,
-0.0289694516279417, -0.0111990899370517, -0.0237924756958046,
0.0304450229355975, 0.00789725649510542, 0.0088295314155904,
-0.0138609782778413), Y = c(1, 0, 0, 0, 0, 0, 1, 0, 0, 1,
1, 1, 1, 0, 0, 0, 1, 1, 1, 0), Y_plus_1 = c(1, 1, 0, 0, 0,
0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1), X1 = c(4.13858526513855,
4.16341131085939, 4.16341131085937, 4.16341131085937, 4.11423119297315,
4.13858526513857, 3.97599968560627, 5.99758130881283, 8.41953801047614,
7.95231443679086, 7.88558780320248, 7.6408950559188, 4.51370117323327,
4.52868963859669, 4.39998987943623, 4.18852747359839, 4.27042958796773,
2.30720560360487, 2.3083029424251, 2.3083029424251), X2 = c(17,
16, 15, 14, 13, 12, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 29, 28, 27), X3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0), X4 = c(1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), X5 = c(0.0793311568823971,
0.0803743155230278, 0.0625789438826206, 0.0586506192715035,
0.0610303101083243, 0.0703939970975855, 0.0867098273608016,
0.0673120522212106, 0.0877296725069155, 0.0923742675361241,
0.0821050363880187, 0.0360531976099817, 0.063410467337928,
0.0289807505667197, 0.0403890038946993, 0.0587200889534704,
0.124855015077667, 0.0636602113103218, 0.0748772236055617,
0.0828248414842617), X6 = c(0.000424348382531349, 0.000403672086504106,
0.000425772306880377, 0.000441567036819891, 0.000440420473928468,
0.000424616565866307, 0.000417156794102717, 0.000453102696396517,
0.000429420158272163, 0.000426339236438714, 0.000424204011080916,
0.000450812884669126, 0.00048728803860348, 0.000526461561504051,
0.000494106517096305, 0.000493488610269819, 0.00041740609044358,
0.000329604373072286, 0.000321981688032803, 0.000313820182149535
), X7 = c(0.633658121909502, 0.779640005424855, 0.37849713571782,
-0.25338461752528, -0.522937755983531, -0.124971717359712,
-0.93339126000489, -0.957987757878853, -1.50800507959919,
-1.67334654587184, -1.6438607078889, -0.962881285608565,
-0.761952591493898, -1.31627449065341, -1.73806924167703,
-2.31894300820554, -2.73548173635386, -2.16326733735933,
-2.18976199245069, -1.70174528306041), X8 = c(-0.861258722930479,
-0.477565916680931, -0.33185021650311, -0.647129621511124,
-0.752564004048953, -0.620377457915967, -1.30234931058582,
-0.793283548875438, -1.16602371553076, -1.20919980603818,
-1.06429334017641, 0.0115994967476536, 1.15288649261686,
0.559547871095353, 0.142148842103466, -0.504045772338972,
-1.18889934824633, 0.593184249578906, 0.999978721521271,
1.3874173248314), X9 = c(-0.0147930666936059, -0.0147662947883565,
-0.0325177535962596, -0.0128572105783758, -0.0126204141222343,
-0.0193506283734377, -0.0204879399440294, -0.015817519858536,
-0.0171950326442131, -0.0146419066547099, -0.0104033279055883,
-0.00702994914474458, 0.012163515055523, 0.0311052843018782,
0.0466972362285693, 0.0638538376999092, 0.0587189119891982,
-0.0382761719744182, 0.017369814913167, 0.00894072522957917
), X10 = c(0.218320938251066, 0.205736199627003, 0.18430441198689,
0.171653942908123, 0.169720213220954, 0.169160112065287,
0.162051896119269, 0.158642308835919, 0.163696353319601,
0.162160340961121, 0.160663336867733, 0.116728767616044,
0.0744707092081847, 0.0816373143043771, 0.120472162777503,
0.120923040165681, 0.10545848677116, 0.172548378854213, 0.184998553367414,
0.181410472295269), X11 = c(0.520408163265306, 0.520408163265306,
0.530612244897959, 0.530612244897959, 0.520408163265306,
0.520408163265306, 0.520408163265306, 0.530612244897959,
0.520408163265306, 0.530612244897959, 0.530612244897959,
0.540816326530612, 0.530612244897959, 0.520408163265306,
0.530612244897959, 0.530612244897959, 0.530612244897959,
0.536082474226804, 0.536082474226804, 0.536082474226804)), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
date = structure(c(17099, 17100, 17101, 17102, 17105, 17106,
17107, 17108, 17109, 17112, 17113, 17114, 17115, 17116, 17119,
17120, 17121, 17122, 17123, 17126), class = "Date"), ID = c("CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1"), var1 = c(807.669983, 799.070007,
795.349976, 795.369995, 784.539978, 783.609985, 768.700012,
762.130005, 762.02002, 782.52002, 790.51001, 785.309998,
762.559998, 754.02002, 736.080017, 758.48999, 764.47998,
771.22998, 760.539978, 769.200012), var2 = c(-0.00669036428079295,
-0.0106478836418512, -0.00465545067066953, 0.0000251700516804565,
-0.0136163258207899, -0.00118539912060411, -0.0190272881732103,
-0.00854690633203736, -0.000144312649125955, 0.0269021803390415,
0.0102105886057713, -0.00657804700031572, -0.0289694516279417,
-0.0111990899370517, -0.0237924756958046, 0.0304450229355975,
0.00789725649510542, 0.0088295314155904, -0.0138609782778413,
0.0113866913646978), Y = c(0, 0, 0, 0, 0, 1, 0, 0, 1, 1,
1, 1, 0, 0, 0, 1, 1, 1, 0, 1), Y_plus_1 = c(1, 0, 0, 0, 0,
0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0), X1 = c(4.16341131085939,
4.16341131085937, 4.16341131085937, 4.11423119297315, 4.13858526513857,
3.97599968560627, 5.99758130881283, 8.41953801047614, 7.95231443679086,
7.88558780320248, 7.6408950559188, 4.51370117323327, 4.52868963859669,
4.39998987943623, 4.18852747359839, 4.27042958796773, 2.30720560360487,
2.3083029424251, 2.3083029424251, 2.30720560360488), X2 = c(16,
15, 14, 13, 12, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
29, 28, 27, 26), X3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0), X4 = c(1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), X5 = c(0.0803743155230278,
0.0625789438826206, 0.0586506192715035, 0.0610303101083243,
0.0703939970975855, 0.0867098273608016, 0.0673120522212106,
0.0877296725069155, 0.0923742675361241, 0.0821050363880187,
0.0360531976099817, 0.063410467337928, 0.0289807505667197,
0.0403890038946993, 0.0587200889534704, 0.124855015077667,
0.0636602113103218, 0.0748772236055617, 0.0828248414842617,
0.0588561607897347), X6 = c(0.000403672086504106, 0.000425772306880377,
0.000441567036819891, 0.000440420473928468, 0.000424616565866307,
0.000417156794102717, 0.000453102696396517, 0.000429420158272163,
0.000426339236438714, 0.000424204011080916, 0.000450812884669126,
0.00048728803860348, 0.000526461561504051, 0.000494106517096305,
0.000493488610269819, 0.00041740609044358, 0.000329604373072286,
0.000321981688032803, 0.000313820182149535, 0.000324018084037671
), X7 = c(0.779640005424855, 0.37849713571782, -0.25338461752528,
-0.522937755983531, -0.124971717359712, -0.93339126000489,
-0.957987757878853, -1.50800507959919, -1.67334654587184,
-1.6438607078889, -0.962881285608565, -0.761952591493898,
-1.31627449065341, -1.73806924167703, -2.31894300820554,
-2.73548173635386, -2.16326733735933, -2.18976199245069,
-1.70174528306041, -2.05934220840334), X8 = c(-0.477565916680931,
-0.33185021650311, -0.647129621511124, -0.752564004048953,
-0.620377457915967, -1.30234931058582, -0.793283548875438,
-1.16602371553076, -1.20919980603818, -1.06429334017641,
0.0115994967476536, 1.15288649261686, 0.559547871095353,
0.142148842103466, -0.504045772338972, -1.18889934824633,
0.593184249578906, 0.999978721521271, 1.3874173248314, 0.798117587221041
), X9 = c(-0.0147662947883565, -0.0325177535962596, -0.0128572105783758,
-0.0126204141222343, -0.0193506283734377, -0.0204879399440294,
-0.015817519858536, -0.0171950326442131, -0.0146419066547099,
-0.0104033279055883, -0.00702994914474458, 0.012163515055523,
0.0311052843018782, 0.0466972362285693, 0.0638538376999092,
0.0587189119891982, -0.0382761719744182, 0.017369814913167,
0.00894072522957917, 0.0159165549773805), X10 = c(0.205736199627003,
0.18430441198689, 0.171653942908123, 0.169720213220954, 0.169160112065287,
0.162051896119269, 0.158642308835919, 0.163696353319601,
0.162160340961121, 0.160663336867733, 0.116728767616044,
0.0744707092081847, 0.0816373143043771, 0.120472162777503,
0.120923040165681, 0.10545848677116, 0.172548378854213, 0.184998553367414,
0.181410472295269, 0.248454355918383), X11 = c(0.520408163265306,
0.530612244897959, 0.530612244897959, 0.520408163265306,
0.520408163265306, 0.520408163265306, 0.530612244897959,
0.520408163265306, 0.530612244897959, 0.530612244897959,
0.540816326530612, 0.530612244897959, 0.520408163265306,
0.530612244897959, 0.530612244897959, 0.530612244897959,
0.536082474226804, 0.536082474226804, 0.536082474226804,
0.530612244897959)), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame")), structure(list(date = structure(c(17100,
17101, 17102, 17105, 17106, 17107, 17108, 17109, 17112, 17113,
17114, 17115, 17116, 17119, 17120, 17121, 17122, 17123, 17126,
17127), class = "Date"), ID = c("CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1"
), var1 = c(799.070007, 795.349976, 795.369995, 784.539978, 783.609985,
768.700012, 762.130005, 762.02002, 782.52002, 790.51001, 785.309998,
762.559998, 754.02002, 736.080017, 758.48999, 764.47998, 771.22998,
760.539978, 769.200012, 768.27002), var2 = c(-0.0106478836418512,
-0.00465545067066953, 0.0000251700516804565, -0.0136163258207899,
-0.00118539912060411, -0.0190272881732103, -0.00854690633203736,
-0.000144312649125955, 0.0269021803390415, 0.0102105886057713,
-0.00657804700031572, -0.0289694516279417, -0.0111990899370517,
-0.0237924756958046, 0.0304450229355975, 0.00789725649510542,
0.0088295314155904, -0.0138609782778413, 0.0113866913646978,
-0.0012090379426567), Y = c(0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1,
0, 0, 0, 1, 1, 1, 0, 1, 0), Y_plus_1 = c(0, 0, 0, 0, 0, 1, 0,
0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1), X1 = c(4.16341131085937,
4.16341131085937, 4.11423119297315, 4.13858526513857, 3.97599968560627,
5.99758130881283, 8.41953801047614, 7.95231443679086, 7.88558780320248,
7.6408950559188, 4.51370117323327, 4.52868963859669, 4.39998987943623,
4.18852747359839, 4.27042958796773, 2.30720560360487, 2.3083029424251,
2.3083029424251, 2.30720560360488, 2.30720560360486), X2 = c(15,
14, 13, 12, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 29, 28,
27, 26, 25), X3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0), X4 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1), X5 = c(0.0625789438826206, 0.0586506192715035,
0.0610303101083243, 0.0703939970975855, 0.0867098273608016, 0.0673120522212106,
0.0877296725069155, 0.0923742675361241, 0.0821050363880187, 0.0360531976099817,
0.063410467337928, 0.0289807505667197, 0.0403890038946993, 0.0587200889534704,
0.124855015077667, 0.0636602113103218, 0.0748772236055617, 0.0828248414842617,
0.0588561607897347, 0.0437146614571738), X6 = c(0.000425772306880377,
0.000441567036819891, 0.000440420473928468, 0.000424616565866307,
0.000417156794102717, 0.000453102696396517, 0.000429420158272163,
0.000426339236438714, 0.000424204011080916, 0.000450812884669126,
0.00048728803860348, 0.000526461561504051, 0.000494106517096305,
0.000493488610269819, 0.00041740609044358, 0.000329604373072286,
0.000321981688032803, 0.000313820182149535, 0.000324018084037671,
0.000321928021838835), X7 = c(0.37849713571782, -0.25338461752528,
-0.522937755983531, -0.124971717359712, -0.93339126000489, -0.957987757878853,
-1.50800507959919, -1.67334654587184, -1.6438607078889, -0.962881285608565,
-0.761952591493898, -1.31627449065341, -1.73806924167703, -2.31894300820554,
-2.73548173635386, -2.16326733735933, -2.18976199245069, -1.70174528306041,
-2.05934220840334, -1.83997395786783), X8 = c(-0.33185021650311,
-0.647129621511124, -0.752564004048953, -0.620377457915967, -1.30234931058582,
-0.793283548875438, -1.16602371553076, -1.20919980603818, -1.06429334017641,
0.0115994967476536, 1.15288649261686, 0.559547871095353, 0.142148842103466,
-0.504045772338972, -1.18889934824633, 0.593184249578906, 0.999978721521271,
1.3874173248314, 0.798117587221041, 0.799993914873595), X9 = c(-0.0325177535962596,
-0.0128572105783758, -0.0126204141222343, -0.0193506283734377,
-0.0204879399440294, -0.015817519858536, -0.0171950326442131,
-0.0146419066547099, -0.0104033279055883, -0.00702994914474458,
0.012163515055523, 0.0311052843018782, 0.0466972362285693, 0.0638538376999092,
0.0587189119891982, -0.0382761719744182, 0.017369814913167, 0.00894072522957917,
0.0159165549773805, -0.00278974160229076), X10 = c(0.18430441198689,
0.171653942908123, 0.169720213220954, 0.169160112065287, 0.162051896119269,
0.158642308835919, 0.163696353319601, 0.162160340961121, 0.160663336867733,
0.116728767616044, 0.0744707092081847, 0.0816373143043771, 0.120472162777503,
0.120923040165681, 0.10545848677116, 0.172548378854213, 0.184998553367414,
0.181410472295269, 0.248454355918383, 0.218320516349615), X11 = c(0.530612244897959,
0.530612244897959, 0.520408163265306, 0.520408163265306, 0.520408163265306,
0.530612244897959, 0.520408163265306, 0.530612244897959, 0.530612244897959,
0.540816326530612, 0.530612244897959, 0.520408163265306, 0.530612244897959,
0.530612244897959, 0.530612244897959, 0.536082474226804, 0.536082474226804,
0.536082474226804, 0.530612244897959, 0.525773195876289)), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame")))
The following should work:
mylist %>%
map(~ mutate_at(.x, vars(starts_with("X")), Scale_Me))
However, you have some columns with sd = 0 where your function does not work.

maping over lists and scaling with the same scale from another list

I have two lists which look like:
[[1]]
<100/1/149>
[[2]]
<100/1/149>
Which can be accessed using:
library(rsample)
map(d, ~ analysis(.x))
map(d, ~ assessment(.x))
It looks like:
[[1]]
# A tibble: 100 x 5
time ID Value out NA
<date> <chr> <dbl> <dbl> <dbl>
1 2016-06-01 CAT1 0 0 0
2 2016-06-02 CAT1 -0.00511 0 0
3 2016-06-03 CAT1 -0.0110 0 0
4 2016-06-06 CAT1 -0.00802 0 0
5 2016-06-07 CAT1 0.000140 0 0
6 2016-06-08 CAT1 0.0162 1 0
7 2016-06-09 CAT1 0.000412 1 1
8 2016-06-10 CAT1 -0.0126 0 1
9 2016-06-13 CAT1 -0.00146 1 0
10 2016-06-14 CAT1 -0.000125 1 1
# ... with 90 more rows
[[2]]
# A tibble: 100 x 5
time ID Value out NA
<date> <chr> <dbl> <dbl> <dbl>
1 2016-06-02 CAT1 -0.00511 0 0
2 2016-06-03 CAT1 -0.0110 0 0
3 2016-06-06 CAT1 -0.00802 0 0
4 2016-06-07 CAT1 0.000140 0 0
5 2016-06-08 CAT1 0.0162 1 0
6 2016-06-09 CAT1 0.000412 1 1
7 2016-06-10 CAT1 -0.0126 0 1
8 2016-06-13 CAT1 -0.00146 1 0
9 2016-06-14 CAT1 -0.000125 1 1
10 2016-06-15 CAT1 0.000905 0 1
# ... with 90 more rows
> map(d, ~ assessment(.x))
[[1]]
# A tibble: 1 x 5
time ID Value out NA
<date> <chr> <dbl> <dbl> <dbl>
1 2016-10-21 CAT1 0.00301 1 0
[[2]]
# A tibble: 1 x 5
time ID Value out NA
<date> <chr> <dbl> <dbl> <dbl>
1 2016-10-24 CAT1 0.0172 1 1
I am trying to apply a centre and scale function to the data.
Scale_Me <- function(x){
(x - mean(x, na.rm = TRUE)) / sd(x, na.rm = TRUE)
}
I want to apply the Scale_Me function to the Value column of each of the analysis lists with the same Scale_Me function being applied to the assessment data. That is, I want to apply the Scale_Me function to list [[1]] A tibble: 100 x 5 and with these values, apply it to [[1]] A tibble: 1 x 5. Then do the same for [[2]] A tibble: 100 x 5 and apply it to [[2]] A tibble: 1 x 5 and so on.
I have been trying with map but cannot seem to get the same scaling to apply to the assessment data.
Data:
list(structure(list(data = structure(list(structure(c(16953,
16954, 16955, 16958, 16959, 16960, 16961, 16962, 16965, 16966,
16967, 16968, 16969, 16972, 16973, 16974, 16975, 16976, 16979,
16980, 16981, 16982, 16983, 16987, 16988, 16989, 16990, 16993,
16994, 16995, 16996, 16997, 17000, 17001, 17002, 17003, 17004,
17007, 17008, 17009, 17010, 17011, 17014, 17015, 17016, 17017,
17018, 17021, 17022, 17023, 17024, 17025, 17028, 17029, 17030,
17031, 17032, 17035, 17036, 17037, 17038, 17039, 17042, 17043,
17044, 17045, 17046, 17050, 17051, 17052, 17053, 17056, 17057,
17058, 17059, 17060, 17063, 17064, 17065, 17066, 17067, 17070,
17071, 17072, 17073, 17074, 17077, 17078, 17079, 17080, 17081,
17084, 17085, 17086, 17087, 17088, 17091, 17092, 17093, 17094,
17095, 17098, 17099, 17100, 17101, 17102, 17105, 17106, 17107,
17108, 17109, 17112, 17113, 17114, 17115, 17116, 17119, 17120,
17121, 17122, 17123, 17126, 17127, 17128, 17130, 17133, 17134,
17135, 17136, 17137, 17140, 17141, 17142, 17143, 17144, 17147,
17148, 17149, 17150, 17151, 17154, 17155, 17156, 17157, 17158,
17162, 17163, 17164, 17165), class = "Date"), c("CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1"), c(0, -0.00510794780005341, -0.0110350448181257,
-0.00801566960652444, 0.000139607845475398, 0.0162282908121412,
0.000411912984092044, -0.0125861865355003, -0.00145951271098099,
-0.00012523665276265, 0.000904900638899031, -0.0119067465120106,
-0.0262402364907984, 0.00287696045138475, 0.00321457082827048,
0.00218412506197607, 0.00632290433988492, -0.0379700289082738,
-0.0103076942313959, 0.0176278212428125, 0.00598495254936315,
0.0116793953826004, 0.0102731487452039, -0.00609260431910685,
0.00405785732974429, -0.0034539102152884, 0.0147693571984875,
0.0134064905587454, 0.00776124374616671, -0.00507886728993256,
0.00553715879207672, -0.00152581452484957, 0.0193513280050455,
0.00433371429355156, 0.00573976860850678, -0.00345390114962729,
0.00556433528583788, -0.00399866715134056, -0.00182494148654477,
0.00453676373490031, 0.00558118134782526, 0.0306739497100141,
0.00532008366009151, -0.00234188747061703, 0.00273643894956987,
-0.00203058539307022, 0.0137504519203442, -0.000588020016175195,
0.00319791236187683, 0.000535515000949838, 0.000216627162048733,
-0.00207683640166156, -0.000995849223563661, -0.00677366569507276,
0.00356429722641427, -0.00309006562735681, -0.00267526302250809,
-0.0042170166770128, -0.0000906650234074879, -0.00316029679084417,
-0.000298895581721914, 0.000168967136587872, 0.00339169643503556,
-0.00396295655622492, -0.00265253602098781, 0.00225544752892959,
0.00348603358425681, 0.011173612052706, 0.000346065780582494,
-0.00644578606355972, -0.0201981554179086, 0.0123213639426545,
-0.0121323473477323, 0.00368569810400099, 0.0121575628815795,
-0.00373173650186931, -0.00413587683295258, 0.00745717762898512,
0.00623533292069589, 0.0141584233987713, -0.000393793258897213,
-0.016126574676531, 0.0113664093074735, -0.00185184350325229,
-0.00838065921587761, 0.00294185619615428, -0.0060852193311054,
0.00500931320547093, 0.0000514895101431101, 0.000502291156859291,
-0.00229123398600595, 0.0140114372217135, -0.00365167187405735,
0.00392047706151, -0.0101127189155992, 0.000436945988930848,
0.00183678592569736, 0.0196163746454174, 0.00784647778278202,
-0.00565193886462889, 0.00301143592272179, 0.0171885235697395,
-0.00669036428079295, -0.0106478836418512, -0.00465545067066953,
0.0000251700516804565, -0.0136163258207899, -0.00118539912060411,
-0.0190272881732103, -0.00854690633203736, -0.000144312649125955,
0.0269021803390415, 0.0102105886057713, -0.00657804700031572,
-0.0289694516279417, -0.0111990899370517, -0.0237924756958046,
0.0304450229355975, 0.00789725649510542, 0.0088295314155904,
-0.0138609782778413, 0.0113866913646978, -0.0012090379426567,
-0.00947587412040363, 0.00090671757719174, 0.00861253683999563,
0.00338440726054889, -0.016605324777718, -0.0133502127773003,
0.00344958960669994, 0.0160160159893405, -0.00447205963195563,
0.0159133949476373, 0.00678170228664343, 0.0165760738798502,
-0.0000252860172512692, 0.00865350998635406, 0.00121847887105075,
0.000978545163097477, -0.00883623264030775, 0.00429947401567232,
0.00279522911918573, -0.00233543235943645, -0.00415322695366804,
-0.00170618631415476, 0.00207620495506755, -0.00821173659091756,
-0.00287881031086645, -0.0140139389980795), c(0, 0, 0, 0, 0,
1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0,
1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0,
0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0,
1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1,
0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0), c(0, 0,
0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1,
1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1,
1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0,
1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0,
1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0,
0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0,
0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-149L), .Names = c("time", "ID", "Value", "out", NA)), in_id = 1:100,
out_id = 101L, id = structure(list(id = "Slice01"), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame"))), class = c("rsplit",
"rof_split")), structure(list(data = structure(list(structure(c(16953,
16954, 16955, 16958, 16959, 16960, 16961, 16962, 16965, 16966,
16967, 16968, 16969, 16972, 16973, 16974, 16975, 16976, 16979,
16980, 16981, 16982, 16983, 16987, 16988, 16989, 16990, 16993,
16994, 16995, 16996, 16997, 17000, 17001, 17002, 17003, 17004,
17007, 17008, 17009, 17010, 17011, 17014, 17015, 17016, 17017,
17018, 17021, 17022, 17023, 17024, 17025, 17028, 17029, 17030,
17031, 17032, 17035, 17036, 17037, 17038, 17039, 17042, 17043,
17044, 17045, 17046, 17050, 17051, 17052, 17053, 17056, 17057,
17058, 17059, 17060, 17063, 17064, 17065, 17066, 17067, 17070,
17071, 17072, 17073, 17074, 17077, 17078, 17079, 17080, 17081,
17084, 17085, 17086, 17087, 17088, 17091, 17092, 17093, 17094,
17095, 17098, 17099, 17100, 17101, 17102, 17105, 17106, 17107,
17108, 17109, 17112, 17113, 17114, 17115, 17116, 17119, 17120,
17121, 17122, 17123, 17126, 17127, 17128, 17130, 17133, 17134,
17135, 17136, 17137, 17140, 17141, 17142, 17143, 17144, 17147,
17148, 17149, 17150, 17151, 17154, 17155, 17156, 17157, 17158,
17162, 17163, 17164, 17165), class = "Date"), c("CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1"), c(0, -0.00510794780005341, -0.0110350448181257,
-0.00801566960652444, 0.000139607845475398, 0.0162282908121412,
0.000411912984092044, -0.0125861865355003, -0.00145951271098099,
-0.00012523665276265, 0.000904900638899031, -0.0119067465120106,
-0.0262402364907984, 0.00287696045138475, 0.00321457082827048,
0.00218412506197607, 0.00632290433988492, -0.0379700289082738,
-0.0103076942313959, 0.0176278212428125, 0.00598495254936315,
0.0116793953826004, 0.0102731487452039, -0.00609260431910685,
0.00405785732974429, -0.0034539102152884, 0.0147693571984875,
0.0134064905587454, 0.00776124374616671, -0.00507886728993256,
0.00553715879207672, -0.00152581452484957, 0.0193513280050455,
0.00433371429355156, 0.00573976860850678, -0.00345390114962729,
0.00556433528583788, -0.00399866715134056, -0.00182494148654477,
0.00453676373490031, 0.00558118134782526, 0.0306739497100141,
0.00532008366009151, -0.00234188747061703, 0.00273643894956987,
-0.00203058539307022, 0.0137504519203442, -0.000588020016175195,
0.00319791236187683, 0.000535515000949838, 0.000216627162048733,
-0.00207683640166156, -0.000995849223563661, -0.00677366569507276,
0.00356429722641427, -0.00309006562735681, -0.00267526302250809,
-0.0042170166770128, -0.0000906650234074879, -0.00316029679084417,
-0.000298895581721914, 0.000168967136587872, 0.00339169643503556,
-0.00396295655622492, -0.00265253602098781, 0.00225544752892959,
0.00348603358425681, 0.011173612052706, 0.000346065780582494,
-0.00644578606355972, -0.0201981554179086, 0.0123213639426545,
-0.0121323473477323, 0.00368569810400099, 0.0121575628815795,
-0.00373173650186931, -0.00413587683295258, 0.00745717762898512,
0.00623533292069589, 0.0141584233987713, -0.000393793258897213,
-0.016126574676531, 0.0113664093074735, -0.00185184350325229,
-0.00838065921587761, 0.00294185619615428, -0.0060852193311054,
0.00500931320547093, 0.0000514895101431101, 0.000502291156859291,
-0.00229123398600595, 0.0140114372217135, -0.00365167187405735,
0.00392047706151, -0.0101127189155992, 0.000436945988930848,
0.00183678592569736, 0.0196163746454174, 0.00784647778278202,
-0.00565193886462889, 0.00301143592272179, 0.0171885235697395,
-0.00669036428079295, -0.0106478836418512, -0.00465545067066953,
0.0000251700516804565, -0.0136163258207899, -0.00118539912060411,
-0.0190272881732103, -0.00854690633203736, -0.000144312649125955,
0.0269021803390415, 0.0102105886057713, -0.00657804700031572,
-0.0289694516279417, -0.0111990899370517, -0.0237924756958046,
0.0304450229355975, 0.00789725649510542, 0.0088295314155904,
-0.0138609782778413, 0.0113866913646978, -0.0012090379426567,
-0.00947587412040363, 0.00090671757719174, 0.00861253683999563,
0.00338440726054889, -0.016605324777718, -0.0133502127773003,
0.00344958960669994, 0.0160160159893405, -0.00447205963195563,
0.0159133949476373, 0.00678170228664343, 0.0165760738798502,
-0.0000252860172512692, 0.00865350998635406, 0.00121847887105075,
0.000978545163097477, -0.00883623264030775, 0.00429947401567232,
0.00279522911918573, -0.00233543235943645, -0.00415322695366804,
-0.00170618631415476, 0.00207620495506755, -0.00821173659091756,
-0.00287881031086645, -0.0140139389980795), c(0, 0, 0, 0, 0,
1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0,
1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0,
0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0,
1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1,
0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0), c(0, 0,
0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1,
1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1,
1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0,
1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0,
1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0,
0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0,
0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-149L), .Names = c("time", "ID", "Value", "out", NA)), in_id = 2:101,
out_id = 102L, id = structure(list(id = "Slice02"), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame"))), class = c("rsplit",
"rof_split")))
EDIT:
For the analysis I have:
[[1]]
# A tibble: 100 x 5
time ID Value out NA
<date> <chr> <dbl> <dbl> <dbl>
1 2016-06-01 CAT1 0 0 0
2 2016-06-02 CAT1 -0.00511 0 0
3 2016-06-03 CAT1 -0.0110 0 0
4 2016-06-06 CAT1 -0.00802 0 0
5 2016-06-07 CAT1 0.000140 0 0
6 2016-06-08 CAT1 0.0162 1 0
7 2016-06-09 CAT1 0.000412 1 1
8 2016-06-10 CAT1 -0.0126 0 1
9 2016-06-13 CAT1 -0.00146 1 0
10 2016-06-14 CAT1 -0.000125 1 1
# ... with 90 more rows
For the assessment I have.
[[1]]
# A tibble: 1 x 5
time ID Value out NA
<date> <chr> <dbl> <dbl> <dbl>
1 2016-10-21 CAT1 0.00301 1 0
I am trying to get it such that these two apply the same scaling function, I have tried to bind_rows() and apply the scaling function and then split it back up again.
If you want to use the scaled analysis values to then scale the assesement values, save them as an intermediate list. Then use purrr::map2() to iterate over assesement and the intermediate list.
library(tidyverse)
scale_values <- map(d,
~ analysis(.x) %>%
as_tibble(., .name_repair = "universal") %>%
summarise(mu = mean(Value, na.rm = TRUE),
sd = sd(Value, na.rm = TRUE)))
scaled_analysis <- map(d,
~ analysis(.x) %>%
as_tibble(., .name_repair = "universal") %>%
mutate(Value = Scale_Me(Value)))
scaled_assessment <- map2(d, scale_values,
~ assessment(.x) %>%
as_tibble(., .name_repair = "universal") %>%
mutate(Value = (Value - .y$mu) / .y$sd))

remove columns which contain whole numbers in r

I have the following data:
# A tibble: 49 x 9
date Y X1 X2 X3 X4 X5 X6 ID
<date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1 2016-10-21 1 4.14 18 0 1 0.0770 0.000429 CAT1
2 2016-10-24 1 4.14 17 0 1 0.0793 0.000424 CAT1
3 2016-10-25 0 4.16 16 0 1 0.0804 0.000404 CAT1
4 2016-10-26 0 4.16 15 0 1 0.0626 0.000426 CAT1
5 2016-10-27 0 4.16 14 0 1 0.0587 0.000442 CAT1
6 2016-10-28 0 4.11 13 0 1 0.0610 0.000440 CAT1
7 2016-10-31 0 4.14 12 0 1 0.0704 0.000425 CAT1
8 2016-11-01 1 3.98 11 0 1 0.0867 0.000417 CAT1
9 2016-11-02 0 6.00 10 0 1 0.0673 0.000453 CAT1
10 2016-11-03 0 8.42 10 0 1 0.0877 0.000429 CAT1
# ... with 39 more rows
I would like to remove all the columns which contain whole numbers - except Y -.
# A tibble: 49 x 6
date Y X1 X5 X6 ID
<date> <dbl> <dbl> <dbl> <dbl> <chr>
1 2016-10-21 1 4.14 0.0770 0.000429 CAT1
2 2016-10-24 1 4.14 0.0793 0.000424 CAT1
3 2016-10-25 0 4.16 0.0804 0.000404 CAT1
4 2016-10-26 0 4.16 0.0626 0.000426 CAT1
5 2016-10-27 0 4.16 0.0587 0.000442 CAT1
6 2016-10-28 0 4.11 0.0610 0.000440 CAT1
7 2016-10-31 0 4.14 0.0704 0.000425 CAT1
8 2016-11-01 1 3.98 0.0867 0.000417 CAT1
9 2016-11-02 0 6.00 0.0673 0.000453 CAT1
10 2016-11-03 0 8.42 0.0877 0.000429 CAT1
# ... with 39 more rows
Data:
df <- structure(list(date = structure(c(17095, 17098, 17099, 17100,
17101, 17102, 17105, 17106, 17107, 17108, 17109, 17112, 17113,
17114, 17115, 17116, 17119, 17120, 17121, 17122, 17123, 17126,
17127, 17128, 17130, 17133, 17134, 17135, 17136, 17137, 17140,
17141, 17142, 17143, 17144, 17147, 17148, 17149, 17150, 17151,
17154, 17155, 17156, 17157, 17158, 17162, 17163, 17164, 17165
), class = "Date"), Y = c(1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1,
1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1,
1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0), X1 = c(4.13858526513854,
4.13858526513855, 4.16341131085939, 4.16341131085937, 4.16341131085937,
4.11423119297315, 4.13858526513857, 3.97599968560627, 5.99758130881283,
8.41953801047614, 7.95231443679086, 7.88558780320248, 7.6408950559188,
4.51370117323327, 4.52868963859669, 4.39998987943623, 4.18852747359839,
4.27042958796773, 2.30720560360487, 2.3083029424251, 2.3083029424251,
2.30720560360488, 2.30720560360486, 2.33467572807035, 2.33467572807036,
2.33467572807036, 2.30720560360486, 2.33467572807035, 2.31545097851707,
2.3399413414153, 2.40685890963718, 2.40309440701756, 2.33633188340289,
2.3363318834029, 2.23996107961566, 2.23996107961567, 2.23996107961566,
3.14644299189703, 3.1910343925295, 3.4393907031427, 3.30413087760388,
3.33080017630688, 2.63827508869038, 2.99443088216722, 2.99443088216723,
2.99443088216722, 2.99443088216722, 3.14542139469794, 3.14542139469794
), X2 = c(18, 17, 16, 15, 14, 13, 12, 11, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19,
18, 17, 16, 15, 14, 13, 12, 11, 10, 10, 10, 10, 10, 87, 86, 85,
84, 83, 82, 81), X3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), X4 = c(1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1), X5 = c(0.0769944316144198, 0.0793311568823971, 0.0803743155230278,
0.0625789438826206, 0.0586506192715035, 0.0610303101083243, 0.0703939970975855,
0.0867098273608016, 0.0673120522212106, 0.0877296725069155, 0.0923742675361241,
0.0821050363880187, 0.0360531976099817, 0.063410467337928, 0.0289807505667197,
0.0403890038946993, 0.0587200889534704, 0.124855015077667, 0.0636602113103218,
0.0748772236055617, 0.0828248414842617, 0.0588561607897347, 0.0437146614571738,
0.0399432627968126, 0.0535895503558405, 0.0538598239712004, 0.0600971764378981,
0.0543532803438423, 0.0412119504402689, 0.0445593481900316, 0.0471324573693227,
0.0366910541674913, 0.0412784111781792, 0.0338162772274317, 0.0391189676384125,
0.051151724195942, 0.0442197922283997, 0.0458769828703159, 0.0392536462039503,
0.0397989336000519, 0.0293505218180493, 0.0229058449521028, 0.0154019371887762,
0.0102366640366435, 0.00783792657548366, 0.014037818210456, 0.00900392496961011,
0.0148108452415051, 0.0159871581537364), X6 = c(0.000428660536007568,
0.000424348382531349, 0.000403672086504106, 0.000425772306880377,
0.000441567036819891, 0.000440420473928468, 0.000424616565866307,
0.000417156794102717, 0.000453102696396517, 0.000429420158272163,
0.000426339236438714, 0.000424204011080916, 0.000450812884669126,
0.00048728803860348, 0.000526461561504051, 0.000494106517096305,
0.000493488610269819, 0.00041740609044358, 0.000329604373072286,
0.000321981688032803, 0.000313820182149535, 0.000324018084037671,
0.000321928021838835, 0.000325886279909115, 0.000324905583026473,
0.000323263064904554, 0.000315582726878559, 0.00033115144688,
0.000326176783596685, 0.000320421043513733, 0.000317459171547033,
0.000306378296724892, 0.000304230982248009, 0.00031578316067723,
0.000315783239223671, 0.000308443856342272, 0.000302387474982801,
0.000302702247056619, 0.000300580888361005, 0.000298662388842681,
0.000295031270763261, 0.000300955138678924, 0.000308009865186193,
0.00031004060452567, 0.000308580481883199, 0.000309081734643359,
0.000309347430761987, 0.00031610525741575, 0.000316030523318374
), ID = c("CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-49L))
One dplyr option could be:
df %>%
select(starts_with("X")) %>%
select_if(~ sum(. %% 1) != 0) %>%
bind_cols(df %>%
select(-starts_with("X")))
X1 X5 X6 date Y ID
<dbl> <dbl> <dbl> <date> <dbl> <chr>
1 4.14 0.0770 0.000429 2016-10-21 1 CAT1
2 4.14 0.0793 0.000424 2016-10-24 1 CAT1
3 4.16 0.0804 0.000404 2016-10-25 0 CAT1
4 4.16 0.0626 0.000426 2016-10-26 0 CAT1
5 4.16 0.0587 0.000442 2016-10-27 0 CAT1
6 4.11 0.0610 0.000440 2016-10-28 0 CAT1
7 4.14 0.0704 0.000425 2016-10-31 0 CAT1
8 3.98 0.0867 0.000417 2016-11-01 1 CAT1
9 6.00 0.0673 0.000453 2016-11-02 0 CAT1
10 8.42 0.0877 0.000429 2016-11-03 0 CAT1
Here is an option using purrr's imap as subsetting vector (original order of columns is preserved).
library(purrr)
not_whole <- function(x,y) {
if (all(is.numeric(x)) & y != "Y") {
!all(as.integer(x) == x)
} else TRUE
}
df[imap_lgl(df, ~ not_whole(.x, .y))]

Resources