remove columns which contain whole numbers in r - 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))]

Related

Creating a variable in longitudinal data dependent on subsequent rows by ID in R

I have the following data:
df<-structure(list(ID = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2), day = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), x1 = c(15, 15, 15.2, 15.2,
15.3, 15.2, 15.3, 15, 15, 15.2, 15.3, 12, 12.1, 12.3, 12.2, 12,
12.4, 12.5, 12.4, 12.6, 12.7), x2 = c(1, 1, 0, 0, 0, 1, 0, 0,
0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -21L))
And I want to generate a variable that indicates a change from 1 to 0 in x2, but only if the following 4 rows remain 0 (by ID). As in the first occurrence of a change in x2 from 1 to 0 for at least 4 days. To generate the variable in this data:
df2<-structure(list(ID = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2), day = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), x1 = c(15, 15, 15.2, 15.2,
15.3, 15.2, 15.3, 15, 15, 15.2, 15.3, 12, 12.1, 12.3, 12.2, 12,
12.4, 12.5, 12.4, 12.6, 12.7), x2 = c(1, 1, 0, 0, 0, 1, 0, 0,
0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1), x3 = c(0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -21L))
Where x3 gets a value of 1 from the first occurrence of when x2 stops for at least 4 days, regardless of re-occurrence
I imagine there is a way to use lag or lead functions in dplyr, but I am not sure how to program the 'at least 4 days' condition.
Any suggestions?
We can use zoo::rollapply for a rolling-window calculation.
fun <- function(z) +(length(z) == 6 && z[1] == 1 && z[2] == 0 && all(z[-(1:2)] == 0))
df %>%
group_by(ID) %>%
mutate(x3a = cummax(zoo::rollapply(lead(x2), 6, fun, fill = 0))) %>%
ungroup()
# # A tibble: 21 x 6
# ID day x1 x2 x3 x3a
# <dbl> <dbl> <dbl> <dbl> <dbl> <int>
# 1 1 1 15 1 0 0
# 2 1 2 15 1 0 0
# 3 1 3 15.2 0 0 0
# 4 1 4 15.2 0 0 0
# 5 1 5 15.3 0 0 0
# 6 1 6 15.2 1 0 0
# 7 1 7 15.3 0 1 1
# 8 1 8 15 0 1 1
# 9 1 9 15 0 1 1
# 10 1 10 15.2 0 1 1
# # ... with 11 more rows
A tidyverse solution could (also) look as follows:
library(dplyr)
library(tidyr)
df %>%
group_by(ID) %>%
mutate(grp = cumsum(x2)) %>%
group_by(ID, grp) %>%
mutate(fourOrMore = n() > 4,
x3 = + lag(fourOrMore),
x3 = replace_na(x3, 0)) %>%
ungroup() %>%
select(- c("grp", "fourOrMore"))
# # A tibble: 21 × 5
# ID day x1 x2 x3
# <dbl> <dbl> <dbl> <dbl> <int>
# 1 1 1 15 1 0
# 2 1 2 15 1 0
# 3 1 3 15.2 0 0
# 4 1 4 15.2 0 0
# 5 1 5 15.3 0 0
# 6 1 6 15.2 1 0
# 7 1 7 15.3 0 1
# 8 1 8 15 0 1
# 9 1 9 15 0 1
# 10 1 10 15.2 0 1
# # … with 11 more rows

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

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)

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))

sum values between start and end dates, for categories, for every day in a period of time in R

I have a set of tasks that have start and end dates. Each task has a category, too. I'd like to specify a particular date range, and sum all of the values within that date range, for each category. I'd be ok with the results ending up in a wide format (results1) or in a long format (results2). If either of those makes this easier, that's fine with me.
I tried to make my example below reproducible.
require(lubridate)
require(dplyr)
require(ggplot2)
dates <- seq(from = ymd("2018-01-01"), to = ymd("2018-01-31"), by = "day") %>%
as_data_frame() %>%
rename(Date = value) %>%
arrange(Date)
tasks <- data.frame(
task = c("task 1", "task 2", "task 3", "task 4"),
category = c("cat1", "cat1", "cat2", "cat2"),
start.date = c(ymd("2018-01-01"), ymd("2018-01-15"), ymd("2018-01-18"), ymd("2018-01-25")),
end.date = c(ymd("2018-01-07"), ymd("2018-01-27"), ymd("2018-02-15"), ymd("2018-01-31")),
value = c(1,3,5,7)
)
# desired results example 1: sums in wide format
results1 <- bind_cols(
dates,
cat1 = c(1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0),
cat2 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 12, 12, 12, 12, 12, 12, 12)
)
# desired results example 2: sums in long format
results2 <- bind_cols(
bind_rows(dates, dates),
category = 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", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2", "cat2"),
value = c(1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 12, 12, 12, 12, 12, 12, 12)
)
#graph the results
ggplot(results2, aes(Date, value, color = category)) + geom_line()
DF1 = do.call(rbind, lapply(split(tasks, tasks$category), function(df1){
do.call(rbind, lapply(dates$Date, function(d){
data.frame(Date = d,
category = df1$category[1],
value = sum(df1$value[d >= df1$start.date & d <= df1$end.date]),
stringsAsFactors = FALSE)
}))
}))
head(DF1)
# Date category value
#cat1.1 2018-01-01 cat1 1
#cat1.2 2018-01-02 cat1 1
#cat1.3 2018-01-03 cat1 1
#cat1.4 2018-01-04 cat1 1
#cat1.5 2018-01-05 cat1 1
#cat1.6 2018-01-06 cat1 1
graphics.off()
ggplot(DF1, aes(Date, value, color = category)) + geom_line()
The solution using sqldf could be:
# Data
require(lubridate)
require(dplyr)
require(ggplot2)
dates <- seq(from = ymd("2018-01-01"), to = ymd("2018-01-31"), by = "day") %>%
as_data_frame() %>%
rename(Date = value) %>%
arrange(Date)
tasks <- data.frame(
task = c("task 1", "task 2", "task 3", "task 4"),
category = c("cat1", "cat1", "cat2", "cat2"),
start_date = c(ymd("2018-01-01"), ymd("2018-01-15"), ymd("2018-01-18"), ymd("2018-01-25")),
end_date = c(ymd("2018-01-07"), ymd("2018-01-27"), ymd("2018-02-15"), ymd("2018-01-31")),
value = c(1,3,5,7)
)
library(sqldf)
# Dates with valid values
result <- sqldf("SELECT dates.Date, tasks.category, sum(tasks.value) as value
FROM dates, tasks
WHERE dates.Date <= tasks.end_date AND
dates.Date >= tasks.start_date
GROUP BY dates.Date, tasks.category")
#Dates with no values for each category is found and joined with result
final_result <- dates %>% merge(unique(result$category)) %>%
mutate(category = y) %>%
anti_join(result, by = c("Date","category")) %>%
mutate(value = 0) %>%
select(-y) %>%
union_all(result) %>%
arrange(category, Date)
final_result
# Date category value
#1 2018-01-01 cat1 1
#2 2018-01-02 cat1 1
#3 2018-01-03 cat1 1
#4 2018-01-04 cat1 1
#5 2018-01-05 cat1 1
#6 2018-01-06 cat1 1
#7 2018-01-07 cat1 1
#8 2018-01-08 cat1 0
#......so on
#57 2018-01-26 cat2 12
#58 2018-01-27 cat2 12
#59 2018-01-28 cat2 12
#60 2018-01-29 cat2 12
#61 2018-01-30 cat2 12
#62 2018-01-31 cat2 12
#plot
ggplot(final_result, aes(Date, value, color = category)) + geom_line()
It's not clear form your results sets what is the goal you're after... but from your description:
I'd like to specify a particular date range, and sum all of the values within that date range, for each category
Then you'd simply group by date range (i.e. by start and end date) and category and summarise the values:
tasks %>%
group_by(category, start.date, end.date) %>%
summarise(value = sum(value))
# A tibble: 4 x 4
# Groups: category, start.date [?]
category start.date end.date value
<fct> <date> <date> <dbl>
1 cat1 2018-01-01 2018-01-07 1.00
2 cat1 2018-01-15 2018-01-27 3.00
3 cat2 2018-01-18 2018-02-15 5.00
4 cat2 2018-01-25 2018-01-31 7.00

Resources