Say that I have a data frame like this....
df <- data.frame(ID = c("2280", "2280","2280","2280","3115","2281", "2281","2281","2281", "3282","3282","3282","3282", "3283","3283","3283","3283","1821","1822", "4007", "1145", "1145", "1146", "1147"), sib_ID = c("2282", "2282", "2282", "2282", "3117", "2282", "2282", "2282", "2282", "3284", "3284", "3284","3284", "3284", "3284", "3284", "3284", "1823", "1823","4009", "1148", "1148","1148", "1148"), Age = c("3", "12", "6", "9", "3","9", "6", "12","3","9", "6", "12","3","9", "6", "12", "6", "12","12", "6", "12","12", "6", "6"), Behavior = c("good", "bad", "good", "bad", "good", "good", "good", "bad", "good", "good", "good", "bad","good", "good", "good", "bad", "good", "bad", "good","good", "bad", "good","good", "good"))
> df
ID sib_ID Age Behavior
1 2280 2282 3 good
2 2280 2282 12 bad
3 2280 2282 6 good
4 2280 2282 9 bad
5 3115 3117 3 good
6 2281 2282 9 good
7 2281 2282 6 good
8 2281 2282 12 bad
9 2281 2282 3 good
10 3282 3284 9 good
11 3282 3284 6 good
12 3282 3284 12 bad
13 3282 3284 3 good
14 3283 3284 9 good
15 3283 3284 6 good
16 3283 3284 12 bad
17 3283 3284 6 good
18 1821 1823 12 bad
19 1822 1823 12 good
20 4007 4009 6 good
21 1145 1148 12 bad
22 1145 1148 12 good
23 1146 1148 6 good
24 1147 1148 6 good
and I want my data frame to only consist of IDs that have a pair. So we would keep data from (2280, 2281), (3282, 3283), (1821, 1822) and remove cases where the ID does not have a pair (3115 and 4007) and cases where we have a triplet (1145, 1146, 1147). What would be the most efficient way to go about doing this?
Example of desired output:
> df
ID sib_ID Age Behavior
1 2280 2282 3 good
2 2280 2282 12 bad
3 2280 2282 6 good
4 2280 2282 9 bad
6 2281 2282 9 good
7 2281 2282 6 good
8 2281 2282 12 bad
9 2281 2282 3 good
10 3282 3284 9 good
11 3282 3284 6 good
12 3282 3284 12 bad
13 3282 3284 3 good
14 3283 3284 9 good
15 3283 3284 6 good
16 3283 3284 12 bad
17 3283 3284 6 good
18 1821 1823 12 bad
19 1822 1823 12 good
First, I think we need to clarify and solidify the grouping of IDs. I suggest we create a frame up-front to clearly identify these groups, then join back into the original data.
GRPs <- data.frame(ID = as.integer(sort(unique(df$ID)))) %>%
mutate(GRP = c(0, cumsum(diff(ID) > 1)), ID = as.character(ID))
GRPs
# ID GRP
# 1 1145 0
# 2 1146 0
# 3 1147 0
# 4 1821 1
# 5 1822 1
# 6 2280 2
# 7 2281 2
# 8 3115 3
# 9 3282 4
# 10 3283 4
# 11 4007 5
From here, we join them back in and then do the grouped determination of "complete" or not.
left_join(df, GRPs, by = "ID") %>%
group_by(ID) %>%
mutate(keep1 = all(c("good", "bad") %in% Behavior)) %>%
group_by(GRP) %>%
mutate(keep2 = all(c("good", "bad") %in% Behavior)) %>%
ungroup() %>%
dplyr::filter(keep1 | keep2)
# # A tibble: 22 × 6
# ID Age Behavior GRP keep1 keep2
# <chr> <chr> <chr> <dbl> <lgl> <lgl>
# 1 2280 3 good 2 TRUE TRUE
# 2 2280 12 bad 2 TRUE TRUE
# 3 2280 6 good 2 TRUE TRUE
# 4 2280 9 bad 2 TRUE TRUE
# 5 2281 9 good 2 TRUE TRUE
# 6 2281 6 good 2 TRUE TRUE
# 7 2281 12 bad 2 TRUE TRUE
# 8 2281 3 good 2 TRUE TRUE
# 9 3282 9 good 4 TRUE TRUE
# 10 3282 6 good 4 TRUE TRUE
# # … with 12 more rows
# # ℹ Use `print(n = ...)` to see more rows
Though it's returning more rows ...
Related
I have 4 data frames that all look like this:
Product 2018
Number
Minimum
Maximum
1
56
1
5
2
42
12
16
3
6523
23
56
4
123
23
102
5
56
23
64
6
245623
56
87
7
546
25
540
8
54566
253
560
Product 2019
Number
Minimum
Maximum
1
56
32
53
2
642
423
620
3
56423
432
560
4
3
431
802
5
2
2
6
6
4523
43
68
7
555
23
54
8
55646
3
6
Product 2020
Number
Minimum
Maximum
1
23
2
5
2
342
4
16
3
223
3
5
4
13
4
12
5
2
4
7
6
223
7
8
7
5
34
50
8
46
3
6
Product 2021
Number
Minimum
Maximum
1
234
3
5
2
3242
4
16
3
2423
43
56
4
123
43
102
5
24
4
6
6
2423
4
18
7
565
234
540
8
5646
23
56
I want to join all the tables so I get a table that looks like this:
Products
Number 2021
Min-Max 2021
Number 2020
Min-Max 2020
Number 2019
Min-Max 2019
Number 2018
Min-Max 2018
1
234
3 to 5
23
2 to 5
...
...
...
...
2
3242
4 to 16
342
4 to 16
...
...
...
...
3
2423
43 to 56
223
3 to 5
...
...
...
...
4
123
43 to 102
13
4 to 12
...
...
...
...
5
24
4 to 6
2
4 to 7
...
...
...
...
6
2423
4 to 18
223
7 to 8
...
...
...
...
7
565
234 to 540
5
34 to 50
...
...
...
...
8
5646
23 to 56
46
3 to 6
...
...
...
...
The Product for all years are the same so I would like to have a data frame that contains the number for each year as a column and joins the column for minimum and maximum as one.
Any help is welcome!
How about something like this. You are trying to join several dataframes by a single column, which is relatively straight forward using full_join. The difficulty is that you are trying to extract information from the column names and combine several columns at the same time. I would map out everying you want to do and then reduce the list of dataframes at the end. Here is an example with two dataframes, but you could add as many as you want to the list at the begining.
library(tidyverse)
#test data
set.seed(23)
df1 <- tibble("Product 2018" = seq(1:8),
Number = sample(1:100, 8),
Minimum = sample(1:100, 8),
Maximum = map_dbl(Minimum, ~sample(.x:1000, 1)))
set.seed(46)
df2 <- tibble("Product 2019" = seq(1:8),
Number = sample(1:100, 8),
Minimum = sample(1:100, 8),
Maximum = map_dbl(Minimum, ~sample(.x:1000, 1)))
list(df1, df2) |>
map(\(x){
year <- str_extract(colnames(x)[1], "\\d+?$")
mutate(x, !!quo_name(paste0("Min-Max ", year)) := paste(Minimum, "to", Maximum))|>
rename(!!quo_name(paste0("Number ", year)) := Number)|>
rename_with(~gsub("\\s\\d+?$", "", .), 1) |>
select(-c(Minimum, Maximum))
}) |>
reduce(full_join, by = "Product")
#> # A tibble: 8 x 5
#> Product `Number 2018` `Min-Max 2018` `Number 2019` `Min-Max 2019`
#> <int> <int> <chr> <int> <chr>
#> 1 1 29 21 to 481 50 93 to 416
#> 2 2 28 17 to 314 78 7 to 313
#> 3 3 72 40 to 787 1 91 to 205
#> 4 4 43 36 to 557 47 55 to 542
#> 5 5 45 70 to 926 52 76 to 830
#> 6 6 34 96 to 645 70 20 to 922
#> 7 7 48 31 to 197 84 6 to 716
#> 8 8 17 86 to 951 99 75 to 768
This is a similar answer, but includes bind_rows to combine the data.frames, then pivot_wider to end in a wide format.
The first steps strip the year from the Product XXXX column name, as this carries relevant information on year for that data.frame. If that column is renamed as Product they are easily combined (with a separate column containing the Year). If this step can be taken earlier in the data collection or processing timeline, it is helpful.
library(tidyverse)
list(df1, df2, df3, df4) %>%
map(~.x %>%
mutate(Year = gsub("Product", "", names(.x)[1])) %>%
rename(Product = !!names(.[1]))) %>%
bind_rows() %>%
mutate(Min_Max = paste(Minimum, Maximum, sep = " to ")) %>%
pivot_wider(id_cols = Product, names_from = Year, values_from = c(Number, Min_Max), names_vary = "slowest")
Output
Product Number_2018 Min_Max_2018 Number_2019 Min_Max_2019 Number_2020 Min_Max_2020 Number_2021 Min_Max_2021
<int> <int> <chr> <int> <chr> <int> <chr> <int> <chr>
1 1 56 1 to 5 56 32 to 53 23 2 to 5 234 3 to 5
2 2 42 12 to 16 642 423 to 620 342 4 to 16 3242 4 to 16
3 3 6523 23 to 56 56423 432 to 560 223 3 to 5 2423 43 to 56
4 4 123 23 to 102 3 431 to 802 13 4 to 12 123 43 to 102
5 5 56 23 to 64 2 2 to 6 2 4 to 7 24 4 to 6
6 6 245623 56 to 87 4523 43 to 68 223 7 to 8 2423 4 to 18
7 7 546 25 to 540 555 23 to 54 5 34 to 50 565 234 to 540
8 8 54566 253 to 560 55646 3 to 6 46 3 to 6 5646 23 to 56
I am looking for a function where I can classify my data into five different industries given their SIC code
Permno SIC Industry
1 854
2 977
3 549
4 1231
5 3295
6 2000
7 1539
8 2549
9 3950
10 4758
11 4290
12 5498
13 5248
14 142
15 3209
16 2759
17 4859
18 2569
19 739
20 4529
It could be that all SICS between 100-200 and 400-700 should be in Industry 1, all SICs between 300-350 and 980-1020 should be in Industry 2 etc.
So in short - an 'If = or' function where I could list all the SICs that could match a given industry
Thank you!
You can add a new column with the filters by number:
For example:
data$Group <- 0
data[data$SCIS < 1000, data$Group == 1]
data[data$SCIS >= 1000, data$Group == 2 ]
floor the value after dividing the SIC value by 1000.
df$Industry <- floor(df$SIC/1000) + 1
df
# Permno SIC Industry
#1 1 854 1
#2 2 977 1
#3 3 549 1
#4 4 1231 2
#5 5 3295 4
#6 6 2000 3
#7 7 1539 2
#8 8 2549 3
#9 9 3950 4
#10 10 4758 5
#11 11 4290 5
#12 12 5498 6
#13 13 5248 6
#14 14 142 1
#15 15 3209 4
#16 16 2759 3
#17 17 4859 5
#18 18 2569 3
#19 19 739 1
#20 20 4529 5
If there is no way to programmatically define groups you may need to individually define the ranges. It is convenient to do this with case_when in dplyr.
library(dplyr)
df %>%
mutate(Industry = case_when(between(SIC, 100, 200) | between(SIC, 400, 700) ~ 'Industry 1',
between(SIC, 300, 350) | between(SIC, 980, 1020) ~ 'Industry 2'))
I have some data with the following features: id, group, sex, datebirth, date1, date2, date3, ctrl1, ctrl2, ctrl3, ab4v1, ab4v2, ab4v3.
What I want is to transform this dataframe onto another one with the following columns in long format: id, group, sex, datebirth, version, date, ctrl, ab4.
(NOTE: version will get values 1, 2 or 3).
Usually, I would use reshape function in R, but I have to use pivot_longer. How could I do this transformation?
I tried things like:
df %>% pivot_longer(cols = -c("id","group","sex","datebirth"),
names_to = c("version",".value"),
names_pattern = "([A-Za-z]+)(\\d+)")
But I get nothing... Any ideas?
Thank you in advance.
This is what I have:
id group sex datebirth date1 date2 date3 ctrl1 ctrl2 ctrl3 ab4v1 ab4v2 ab4v3
1 1 A Male 1975-01-08 2010-10-10 2011-11-12 2011-12-12 183 835 139 745 584 817
2 2 B Male 1998-05-12 2010-10-10 2011-11-12 2011-12-12 172 727 214 793 653 499
3 3 A Male 2005-12-28 2010-10-10 2011-11-23 2011-12-23 157 667 222 664 505 924
4 4 C Female 1957-07-01 2010-10-10 2011-11-25 2011-12-25 186 123 344 584 582 653
This is what I want:
id group sex datebirth version date ctrl ab4
1 1 A Male 1975-01-08 1 2010-10-10 183 745
2 2 B Male 1998-05-12 1 2010-10-10 172 793
3 3 A Male 2005-12-28 1 2010-10-10 157 664
4 4 C Female 1957-07-01 1 2010-10-10 186 584
.........
We need the change the order of names_to. We could either use names_sep or names_pattern. The only difference is that names_sep directs to a delimiter. Here the delimiter is the boundary between a letter ((?<=[A-Za-z])) and a digit ((?=[0-9]$)). Here, it means check for the boundary that succeeds a letter and precedes a digit. With the names_pattern, we are capturing specific sets of characters in a group ((...)). The OP's post used that "([A-Za-z]+)(\\d+)" i.e. one or more letters as the first group and digits as the second group.
library(dplyr)
library(tidyr)
df %>%
pivot_longer(cols = date1:ab4v3, names_to = c(".value", "version"),
names_sep = "(?<=[A-Za-z])(?=[0-9]$)")
# A tibble: 12 x 8
# id group sex datebirth version date ctrl ab4v
# <int> <chr> <chr> <chr> <chr> <chr> <int> <int>
# 1 1 A Male 1975-01-08 1 2010-10-10 183 745
# 2 1 A Male 1975-01-08 2 2011-11-12 835 584
# 3 1 A Male 1975-01-08 3 2011-12-12 139 817
# 4 2 B Male 1998-05-12 1 2010-10-10 172 793
# 5 2 B Male 1998-05-12 2 2011-11-12 727 653
# 6 2 B Male 1998-05-12 3 2011-12-12 214 499
# 7 3 A Male 2005-12-28 1 2010-10-10 157 664
# 8 3 A Male 2005-12-28 2 2011-11-23 667 505
# 9 3 A Male 2005-12-28 3 2011-12-23 222 924
#10 4 C Female 1957-07-01 1 2010-10-10 186 584
#11 4 C Female 1957-07-01 2 2011-11-25 123 582
#12 4 C Female 1957-07-01 3 2011-12-25 344 653
data
df <- structure(list(id = 1:4, group = c("A", "B", "A", "C"), sex = c("Male",
"Male", "Male", "Female"), datebirth = c("1975-01-08", "1998-05-12",
"2005-12-28", "1957-07-01"), date1 = c("2010-10-10", "2010-10-10",
"2010-10-10", "2010-10-10"), date2 = c("2011-11-12", "2011-11-12",
"2011-11-23", "2011-11-25"), date3 = c("2011-12-12", "2011-12-12",
"2011-12-23", "2011-12-25"), ctrl1 = c(183L, 172L, 157L, 186L
), ctrl2 = c(835L, 727L, 667L, 123L), ctrl3 = c(139L, 214L, 222L,
344L), ab4v1 = c(745L, 793L, 664L, 584L), ab4v2 = c(584L, 653L,
505L, 582L), ab4v3 = c(817L, 499L, 924L, 653L)), class = "data.frame",
row.names = c("1",
"2", "3", "4"))
The following is ugly but I believe it might work. It's a sequence of pivot_longer statements, taking care of one variable in wide format at a time.
library(dplyr)
library(tidyr)
fun <- function(X, Var){
Vard <- paste0(Var, "\\d")
X %>%
select(1:4, matches( {{ Vard }} )) %>%
pivot_longer(
cols = matches( {{ Vard }} ),
names_to = "version",
values_to = Var
) %>%
mutate(version = sub(Var, "", version))
}
vars <- c("date", "ctrl", "ab4v")
Reduce(function(x, y) merge(x, y), lapply(vars, function(v) fun(df1, v)))
# id group sex datebirth version date ctrl ab4v
#1 1 A Male 1975-01-08 1 2010-10-10 183 745
#2 1 A Male 1975-01-08 2 2011-11-12 835 584
#3 1 A Male 1975-01-08 3 2011-12-12 139 817
#4 2 B Male 1998-05-12 1 2010-10-10 172 793
#5 2 B Male 1998-05-12 2 2011-11-12 727 653
#6 2 B Male 1998-05-12 3 2011-12-12 214 499
#7 3 A Male 2005-12-28 1 2010-10-10 157 664
#8 3 A Male 2005-12-28 2 2011-11-23 667 505
#9 3 A Male 2005-12-28 3 2011-12-23 222 924
#10 4 C Female 1957-07-01 1 2010-10-10 186 584
#11 4 C Female 1957-07-01 2 2011-11-25 123 582
#12 4 C Female 1957-07-01 3 2011-12-25 344 653
I have a data frame with thousands of rows looking like this:
time Unique_ID Unix_Time Event Version
<dbl> <dbl> <dbl> <lgl> <dbl>
1 1404 4961657804 1565546745 FALSE 6
2 2534 4453645779 1550934792 FALSE 5
3 2114 3602935494 1512593418 TRUE 3
4 2605 5343699852 1586419012 TRUE 6
5 1246 5095942046 1572689498 FALSE 6
6 2519 3206995213 1495881898 TRUE 3
7 1419 4958551504 1565434177 TRUE 6
8 2262 5441937631 1590754817 TRUE 6
9 1650 3024892331 1488210079 TRUE 2
10 1880 3163703804 1494173662 FALSE 2
I manipulate the data frame using the following command:
df <- df %>%
group_by(minute = findInterval(time, seq(min(0), max(9000), 60))) %>%
summarise(Number= n(),
Won = sum(Event))
Now my data frame looks like this:
minute Number Won
<int> <int> <int>
1 55 264 128
2 71 34 17
3 31 1427 728
4 80 9 5
5 24 1197 673
6 141 1 1
7 53 326 163
8 30 1572 802
9 77 14 9
10 97 1 1
I would want something like this though:
minute Number Won Version
<int> <int> <int> <int>
1 55 264 128 1
2 55 34 17 2
3 55 1427 728 3
4 80 9 5 1
5 24 1197 673 1
6 141 1 1 2
7 53 326 163 3
8 53 1572 802 4
9 77 14 9 2
10 97 1 1 6
Is it possible to keep the rows with different Versions seperated while grouping time?
I think you can group by 2 columns: minute and Version
df <- df %>%
group_by(minute = findInterval(time, seq(min(0), max(9000), 60)), Version)
Starting from this SO question.
Example data.frame:
df = read.table(text = 'ID Day Count Count_group
18 1933 6 15
33 1933 6 15
37 1933 6 15
18 1933 6 15
16 1933 6 15
11 1933 6 15
111 1932 5 9
34 1932 5 9
60 1932 5 9
88 1932 5 9
18 1932 5 9
33 1931 3 4
13 1931 3 4
56 1931 3 4
23 1930 1 1
6 1800 6 12
37 1800 6 12
98 1800 6 12
52 1800 6 12
18 1800 6 12
76 1800 6 12
55 1799 4 6
6 1799 4 6
52 1799 4 6
133 1799 4 6
112 1798 2 2
677 1798 2 2
778 888 4 8
111 888 4 8
88 888 4 8
10 888 4 8
37 887 2 4
26 887 2 4
8 886 1 2
56 885 1 1
22 120 2 6
34 120 2 6
88 119 1 6
99 118 2 5
12 118 2 5
90 117 1 3
22 115 2 2
99 115 2 2', header = TRUE)
The Count col shows the total number of ID values per each Day and the Count_group col shows the sum of the ID values per each Day, Day - 1, Day -2, Day -3 and Day -4.
e.g. 1933 = Count_group 15 because Count 6 (1933) + Count 5 (1932) + Count 3 (1931) + Count 1 (1930) + Count 0 (1929).
What I need to do is to create duplicated observations per each Count_group and add them to it in order to show per each Count_group its Day, Day - 1, Day -2, Day -3 and Day -4.
e.g. Count_group = 15 is composed by the Count values of Day 1933, 1932, 1931, 1930 (and 1929 not present in the df). So the five days needs to be included in the Count_group = 15. The next one will be Count_group = 9, composed by 1932, 1931, 1930, 1929 and 1928; etc...
Desired output:
ID Day Count Count_group
18 1933 6 15
33 1933 6 15
37 1933 6 15
18 1933 6 15
16 1933 6 15
11 1933 6 15
111 1932 5 15
34 1932 5 15
60 1932 5 15
88 1932 5 15
18 1932 5 15
33 1931 3 15
13 1931 3 15
56 1931 3 15
23 1930 1 15
111 1932 5 9
34 1932 5 9
60 1932 5 9
88 1932 5 9
18 1932 5 9
33 1931 3 9
13 1931 3 9
56 1931 3 9
23 1930 1 9
33 1931 3 4
13 1931 3 4
56 1931 3 4
23 1930 1 4
23 1930 1 1
6 1800 6 12
37 1800 6 12
98 1800 6 12
52 1800 6 12
18 1800 6 12
76 1800 6 12
55 1799 4 12
6 1799 4 12
52 1799 4 12
133 1799 4 12
112 1798 2 12
677 1798 2 12
55 1799 4 6
6 1799 4 6
52 1799 4 6
133 1799 4 6
112 1798 2 6
677 1798 2 6
112 1798 2 2
677 1798 2 2
778 888 4 8
111 888 4 8
88 888 4 8
10 888 4 8
37 887 2 8
26 887 2 8
8 886 1 8
56 885 1 8
37 887 2 4
26 887 2 4
8 886 1 4
56 885 1 4
8 886 1 2
56 885 1 2
56 885 1 1
22 120 2 6
34 120 2 6
88 119 1 6
99 118 2 6
12 118 2 6
90 117 1 6
88 119 1 6
99 118 2 6
12 118 2 6
90 117 1 6
22 115 2 6
99 115 2 6
99 118 2 5
12 118 2 5
90 117 1 5
22 115 2 5
99 115 2 5
90 117 1 3
22 115 2 3
99 115 2 3
22 115 2 2
99 115 2 2
(note that different group of 5 days each one have been separated by a blank line in order to make them clearer)
I have got different data.frames which are grouped by n days and therefore I would like to adapt the code (by changing it a little) specifically for each of them.
Thanks
A generalised version of my previous answer...
#first add grouping variables
days <- 5 #grouping no of days
df$smalldaygroup <- c(0,cumsum(sapply(2:nrow(df),function(i) df$Day[i]!=df$Day[i-1]))) #individual days
df$bigdaygroup <- c(0,cumsum(sapply(2:nrow(df),function(i) df$Day[i]<df$Day[i-1]-days+1))) #blocks of linked days
#duplicate days in each big group
df2 <- lapply(split(df,df$bigdaygroup),function(x) {
n <- max(x$Day)-min(x$Day)+1 #number of consecutive days in big group
dayvec <- (max(x$Day):min(x$Day)) #possible days in range
daylog <- dayvec[dayvec %in% x$Day] #actual days in range
pattern <- data.frame(base=rep(dayvec,each=days))
pattern$rep <- sapply(1:nrow(pattern),function(i) pattern$base[i]+1-sum(pattern$base[1:i]==pattern$base[i])) #indices to repeat
pattern$offset <- match(pattern$rep,daylog)-match(pattern$base,daylog) #offsets (used later)
pattern <- pattern[(pattern$base %in% x$Day) & (pattern$rep %in% x$Day),] #remove invalid elements
#store pattern in list as offsets needed in next loop
return(list(df=split(x,x$smalldaygroup)[match(pattern$rep,daylog)],pat=pattern))
})
#change the Count_group to previous value in added entries
df2 <- lapply(df2,function(L) lapply(1:length(L$df),function(i) {
x <- L$df[[i]]
offset <- L$pat$offset #pointer to day to copy Count_group from
x$Count_group <- L$df[[i-offset[i]]]$Count_group[1]
return(x)
}))
df2 <- do.call(rbind,unlist(df2,recursive=FALSE)) #bind back together
df2[,5:6] <- NULL #remove grouping variables
head(df2,30) #ignore rownames!
ID Day Count Count_group
01.1 18 1933 6 15
01.2 33 1933 6 15
01.3 37 1933 6 15
01.4 18 1933 6 15
01.5 16 1933 6 15
01.6 11 1933 6 15
02.7 111 1932 5 15
02.8 34 1932 5 15
02.9 60 1932 5 15
02.10 88 1932 5 15
02.11 18 1932 5 15
03.12 33 1931 3 15
03.13 13 1931 3 15
03.14 56 1931 3 15
04 23 1930 1 15
05.7 111 1932 5 9
05.8 34 1932 5 9
05.9 60 1932 5 9
05.10 88 1932 5 9
05.11 18 1932 5 9
06.12 33 1931 3 9
06.13 13 1931 3 9
06.14 56 1931 3 9
07 23 1930 1 9
08.12 33 1931 3 4
08.13 13 1931 3 4
08.14 56 1931 3 4
09 23 1930 1 4
010 23 1930 1 1
11.16 6 1800 6 12
I attach a rather mechanical method, but I believe it is a good starting point.
I have noticed that in your original table the entry
ID Day Count Count_group
18 1933 6 14
is duplicated; I have left it untouched for sake of clarity.
Structure of the approach:
Read original data
Generate list of data frames, for each Day
Generate final data frame, collapsing the list in 2.
1. Read original data
We start with
df = read.table(text = 'ID Day Count Count_group
18 1933 6 14
33 1933 6 14
37 1933 6 14
18 1933 6 14
16 1933 6 14
11 1933 6 14
111 1932 5 9
34 1932 5 9
60 1932 5 9
88 1932 5 9
18 1932 5 9
33 1931 3 4
13 1931 3 4
56 1931 3 4
23 1930 1 1
6 1800 6 12
37 1800 6 12
98 1800 6 12
52 1800 6 12
18 1800 6 12
76 1800 6 12
55 1799 4 6
6 1799 4 6
52 1799 4 6
133 1799 4 6
112 1798 2 2
677 1798 2 2
778 888 4 7
111 888 4 7
88 888 4 7
10 888 4 7
37 887 2 4
26 887 2 4
8 886 1 2
56 885 1 1', header = TRUE)
# ordered vector of unique values for "Day"
ord_day <- unique(df$Day[order(df$Day)])
ord_day
[1] 885 886 887 888 1798 1799 1800 1930 1931 1932 1933
2. Generate list of data frames, for each Day
For each element in ord_day we introduce a data.frame as element of a list called df_new_aug.
Such data frames are defined through a for loop for all values in ord_day except ord_day[2] and ord_day[1] which are treated separately.
Idea behind the looping: for each unique ord_day[i] with i > 2 we check which days between ord_day[i-1] and ord_day[i-2] (or both!) contribute (through the variable "Count") to the value "Count_Group" at ord_day[i].
We therefore introduce if else statements in the loop.
Here we go
# Recursive generation of the list of data.frames (for days > 886)
#-----------------------------------------------------------------
df_new <- list()
df_new_aug <- list()
# we exclude cases i=1, 2: they are manually treated below
for ( i in 3: length(ord_day) ) {
# is "Count_Group" for ord_day[i] equal to the sum of "Count" at ord_day[i-1] and ord_day[i-2]?
if ( unique(df[df$Day == ord_day[i], "Count_group"]) == unique(df[df$Day == ord_day[i], "Count"]) +
unique(df[df$Day == ord_day[i-1], "Count"]) + unique(df[df$Day == ord_day[i-2], "Count"])
) {
# we create columns ID | Day | Count
df_new[[i]] <- data.frame(df[df$Day == ord_day[i] | df$Day == ord_day[i-1] | df$Day == ord_day[i-2],
c("ID", "Day", "Count")])
# we append the Count_Group of the Day in ord_day[i]
df_new_aug[[i]] <- data.frame( df_new[[i]],
Count_group = rep(unique(df[df$Day == ord_day[i], "Count_group"]), nrow(df_new[[i]]) ) )
} else if (unique(df[df$Day == ord_day[i], "Count_group"]) == unique(df[df$Day == ord_day[i], "Count"]) +
unique(df[df$Day == ord_day[i-1], "Count"]) ) #only "Count" at i and i-1 contribute to "Count_group" at i
{
df_new[[i]] <- data.frame(df[df$Day == ord_day[i] | df$Day == ord_day[i-1],
c("ID", "Day", "Count")])
# we append the Count_Group of the Day in ord_day[2]
df_new_aug[[i]] <- data.frame(df_new[[i]],
Count_group = rep(unique(df[df$Day == ord_day[i], "Count_group"]), nrow(df_new[[i]]) ) )
} else #only "Count" at i contributes to "Count_group" at i
df_new[[i]] <- data.frame(df[df$Day == ord_day[i],
c("ID", "Day", "Count")])
# we append the Count_Group of the Day in ord_day[i]
df_new_aug[[i]] <- data.frame(df_new[[i]],
Count_group = rep(unique(df[df$Day == ord_day[i], "Count_group"]), nrow(df_new[[i]]) ) )
#closing the for loop
}
# for ord_day[2] = "886" (both "Count" at i =2 and i = 1 contribute to "Count_group" at i=2)
#-------------------------------------------------------------------------------------
df_new[[2]] <- data.frame(df[df$Day == ord_day[2] | df$Day == ord_day[1],
c("ID", "Day", "Count")])
# we append the Count_Group of the Day in ord_day[2]
df_new_aug[[2]] <- data.frame(df_new[[2]],
Count_group = rep(unique(df[df$Day == ord_day[2], "Count_group"]), nrow(df_new[[2]]) ) )
# for ord_day[1] = "885" (only "count" at i = 1 contributes to "Count_group" at i =1)
#------------------------------------------------------------------------------------
df_new[[1]] <- data.frame(df[df$Day == ord_day[1], c("ID", "Day", "Count")])
# we append the Count_Group of the Day in ord_day[i]
df_new_aug[[1]] <- data.frame(df_new[[1]], Count_group = rep(unique(df[df$Day == ord_day[1], "Count_group"]), nrow(df_new[[1]]) ) )
# produced list
df_new_aug
3. Generate final data frame, collapsing the list in 2.
We collapse df_new_aug through an ugly loop, but other solutions (for example with Reduce() and merge() are possible):
# merging the list (mechanically): final result
df_result <- df_new_aug[[1]]
for (i in 1:10){
df_result <- rbind(df_result, df_new_aug[[i+1]])
}
One arrives at df_result and the analysis is stopped.